QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#92174#5717. 密码锁myee100 ✓1828ms41592kbC++115.9kb2023-03-30 12:42:532023-03-30 12:42:55

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-30 12:42:55]
  • 评测
  • 测评结果:100
  • 用时:1828ms
  • 内存:41592kb
  • [2023-03-30 12:42:53]
  • 提交

answer

// 那就是希望。
// 即便需要取模,也是光明。

#include <algorithm>
#include <stdio.h>
#include <vector>
typedef long long llt;
typedef unsigned uint;typedef unsigned long long ullt;
typedef bool bol;typedef char chr;typedef void voi;
typedef double dbl;
template<typename T>bol _max(T&a,T b){return(a<b)?a=b,true:false;}
template<typename T>bol _min(T&a,T b){return(b<a)?a=b,true:false;}
template<typename T>T lowbit(T n){return n&-n;}
template<typename T>T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<typename T>T lcm(T a,T b){return(a!=0||b!=0)?a/gcd(a,b)*b:(T)0;}
template<typename T>T exgcd(T a,T b,T&x,T&y){if(b!=0){T ans=exgcd(b,a%b,y,x);y-=a/b*x;return ans;}else return y=0,x=1,a;}
template<typename T>T power(T base,T index,T mod)
{
    T ans=1%mod;
    while(index)
    {
        if(index&1)ans=ans*base%mod;
        base=base*base%mod,index>>=1;
    }
    return ans;
}
struct Seg{
    Seg*L,*R;uint len,tag,v;
    Seg(uint n):L(NULL),R(NULL),len(n),tag(0),v(0){if(n>1)L=new Seg(n>>1),R=new Seg(n-(n>>1));}
    voi clear(){tag=v=0;if(len>1)L->clear(),R->clear();}
    voi add(uint l,uint r,uint w){
        if(l>=r)return;
        if(!l&&r==len){tag+=w,v+=w;return;}
        if(l<(len>>1))
            if(r<=(len>>1))L->add(l,r,w);
            else L->add(l,len>>1,w),R->add(0,r-(len>>1),w);
        else R->add(l-(len>>1),r-(len>>1),w);
        v=std::max(L->v,R->v)+tag;
    }
};
uint A[50005][4],T[200005],Back[200005],n,k;
uint solve3(uint u){
    auto check=[&](uint v)
    {
        static bol Gone[200005];
        static uint Cnt[50005];
        for(uint i=0;i<n*k;i++)Gone[i]=0;
        for(uint i=0;i<n;i++)Cnt[i]=0;
        for(uint i=0;i<n;i++)for(uint j=0;j<k;j++)
            if(T[A[i][j]]-T[0]<=v&&T[n*k-1]-T[A[i][(u+j)%k]]<=v)
                Gone[A[i][((u^3)+j)%k]]=1;
        for(uint i=0,j=0,cnt=0;i<n*k;i++)if(Gone[i]){
            while(T[i]-T[j]>v){
                if(Gone[j]&&!--Cnt[Back[j]])cnt--;
                j++;
            }
            if(!Cnt[Back[i]]++)cnt++;
            if(cnt==n)return true;
        }
        return false;
    };
    uint l=0,r=T[n*k-1]-T[0];
    while(l<r){uint mid=(l+r)>>1;if(check(mid))r=mid;else l=mid+1;}
    return l;
}
uint solve4(uint u){
    Seg S(n*k);
    auto check=[&](uint v)
    {
        S.clear();
        static bol Gone[200005];
        static uint L[200005];
        static std::vector<std::pair<uint,uint> >V[50005];
        for(uint i=0;i<n*k;i++){
            Gone[i]=false;uint&l=L[i]=0,r=i;
            while(l<r){uint mid=(l+r)>>1;if(T[i]-T[mid]<=v)r=mid;else l=mid+1;}
        }
        for(uint i=0;i<n;i++)V[i].clear();
        for(uint i=0;i<n;i++){
            for(uint j=0;j<k;j++)if(T[A[i][j]]-T[0]<=v&&T[n*k-1]-T[A[i][(u+j)%k]]<=v){
                std::vector<uint>B;
                for(uint t=1;t<k;t++)if(t!=u)B.push_back(A[i][(t+j)%k]);
                V[i].push_back({B[0],B[1]});
            }
            if(V[i].empty())return false;
            for(auto s:V[i])Gone[s.first]=true;
            std::sort(V[i].begin(),V[i].end(),[&](std::pair<uint,uint>a,
                        std::pair<uint,uint>b){return a.second<b.second;});
        }
        for(uint i=0,j=0;i<n*k;i++)if(Gone[i]){
            auto insert=[&](uint id,uint l,uint r)
            {
                std::vector<std::pair<uint,uint> >W;
                for(auto p:V[id])if(p.first>=l&&p.first<r){
                    if(W.empty()||W.back().second<L[p.second])
                        W.push_back({L[p.second],p.second+1});
                    else W.back().second=p.second+1;
                }
                for(auto s:W)S.add(s.first,s.second,1);
            };
            auto kill=[&](uint id,uint l,uint r)
            {
                std::vector<std::pair<uint,uint> >W;
                for(auto p:V[id])if(p.first>=l&&p.first<r){
                    if(W.empty()||W.back().second<L[p.second])
                        W.push_back({L[p.second],p.second+1});
                    else W.back().second=p.second+1;
                }
                for(auto s:W)S.add(s.first,s.second,-1);
            };
            while(T[i]-T[j]>v){
                if(Gone[j])kill(Back[j],j,i),insert(Back[j],j+1,i);
                j++;
            }
            kill(Back[i],j,i),insert(Back[i],j,i+1);
            if(S.v==n)return true;
        }
        return false;
    };
    uint l=0,r=T[n*k-1]-T[0];
    while(l<r){uint mid=(l+r)>>1;if(check(mid))r=mid;else l=mid+1;}
    return l;
}
voi solve(){
    scanf("%u",&n);
    for(uint i=0;i<k;i++)for(uint j=0;j<n;j++)scanf("%u",A[j]+i),T[i*n+j]=i*n+j;
    std::sort(T,T+n*k,[&](uint u,uint v){return A[u%n][u/n]<A[v%n][v/n];});
    for(uint i=0,p;i<n*k;i++)p=T[i],std::swap(A[p%n][p/n],T[i]=i);
    if(k==1){
        printf("%u\n",T[n*k-1]-T[0]);
        return;
    }
    if(k==2){
        for(uint i=0;i<n;i++)if(A[i][0]>A[i][1])std::swap(A[i][0],A[i][1]);
        uint Max=0,Min=-1;
        for(uint i=0;i<n;i++)_max(Max,A[i][0]),_min(Min,A[i][1]);
        printf("%u\n",std::max(T[Max]-T[0],T[n*k-1]-T[Min]));
        return;
    }
    for(uint i=0;i<n;i++)for(uint j=1;j<k;j++)if(!A[i][j]){
        std::vector<uint>B(k);
        for(uint t=0;t<k;t++)B[t]=A[i][(t+j)%k];
        for(uint t=0;t<k;t++)A[i][t]=A[0][t],A[0][t]=B[t];
        break;
    }
    for(uint i=0;i<n;i++)for(uint j=0;j<k;j++)Back[A[i][j]]=i;
    if(k==3){
        uint ans=-1;
        for(uint u=1;u<k;u++)_min(ans,solve3(u));
        printf("%u\n",ans);
        return;
    }
    if(k==4){
        uint ans=-1;
        for(uint u=1;u<k;u++)_min(ans,solve4(u));
        printf("%u\n",ans);
        return;
    }
}
int main()
{
#ifdef MYEE
    freopen("QAQ.in","r",stdin);
    freopen("QAQ.out","w",stdout);
#else
#endif
    uint t;scanf("%u%u",&t,&k);
    while(t--)solve();
    return 0;
}

// 那就是希望。
// 即便需要取模,也是光明。

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 5
Accepted
time: 3ms
memory: 5144kb

input:

5 1
20
20828 22671 20646 13184 13779 12445 15192 14863 14396 19238 16788 20325 16831 8559 13417 8768 13227 8760 12456 12091
20
17138 10757 10189 4459 19805 4791 8043 15409 14758 15901 9004 13481 20599 17218 14056 5078 8021 16969 22031 15690
20
26410 25246 16063 9589 20080 21752 25766 14007 12750 231...

output:

14112
17572
16821
21321
16199

result:

ok 5 number(s): "14112 17572 16821 21321 16199"

Test #2:

score: 5
Accepted
time: 34ms
memory: 5644kb

input:

3 1
50000
15429 14039 16730 23945 7044 17994 15646 15950 7744 7726 19830 8000 7607 19562 22681 17139 19133 13561 14539 21246 8476 20813 21139 18999 18270 11950 19511 14822 24627 20644 10619 7699 8058 9578 8425 12058 22166 12276 21095 22645 8087 24264 21082 16567 18500 15489 14629 16114 19826 22286 1...

output:

18373
18000
18000

result:

ok 3 number(s): "18373 18000 18000"

Test #3:

score: 5
Accepted
time: 2ms
memory: 3128kb

input:

5 2
20
3646 6903 10939 9491 14584 16498 7415 10353 8781 10454 703 4651 11986 3700 2186 21659 18792 6774 15283 9422
22629 3204 24229 11810 15271 20496 13170 19867 6105 13572 6657 11440 22722 22564 10168 2797 5798 11753 18320 12720
20
18619 25811 14645 26890 3897 11154 16664 20111 15847 16651 8537 116...

output:

17572
18784
14814
14107
15187

result:

ok 5 number(s): "17572 18784 14814 14107 15187"

Test #4:

score: 5
Accepted
time: 0ms
memory: 5184kb

input:

186 2
5
10809 20957 1456 13581 19134
19871 12718 14451 14783 23041
7
24553 4790 18932 21045 14061 6438 17183
9155 15756 16480 17055 13768 11550 15400
4
12340 23115 28018 8781
16634 10828 1457 21446
7
9619 7632 22338 17091 11090 3368 4366
17684 14282 25007 9194 3144 9944 15465
3
3024 25036 17327
2034...

output:

17678
13003
11384
19194
20499
16143
15079
11508
11891
22699
12676
11250
16873
3238
9264
8040
18134
10772
27251
15001
14812
6575
5499
18396
18795
10752
17672
13273
13915
15178
8303
15337
11707
18835
16270
10944
15953
22359
4914
17434
19001
3224
14155
17053
8670
17906
18767
3696
10036
10580
23062
7636...

result:

ok 186 numbers

Test #5:

score: 5
Accepted
time: 6ms
memory: 5168kb

input:

1357 2
6
1679 1896 8067 24584 3225 2239
14688 13434 4956 688 2751 5016
5
18325 12446 17937 6311 21564
24096 6172 17497 25829 12016
3
9440 752 22972
11570 26214 5969
3
13475 23941 17551
9287 14467 15412
2
20079 827
5314 5994
3
24501 13180 9614
25780 17665 18145
4
16980 22134 5840 14181
16144 7553 773...

output:

21359
13383
14644
10466
14085
14887
14396
19769
12727
14894
9957
19961
16696
14903
12494
14811
13441
15241
5475
15280
10050
15477
10792
15489
14676
21437
19598
12192
15812
10749
24133
15641
24351
20130
8512
11012
19409
7014
18531
9567
17379
4718
13822
15877
11260
17467
12318
12804
19625
4182
20704
1...

result:

ok 1357 numbers

Test #6:

score: 5
Accepted
time: 76ms
memory: 5224kb

input:

3 2
50000
7513 17243 9697 14636 12922 14859 12193 5056 13705 14408 13089 11378 14342 14499 15298 9625 22287 6924 7449 7005 13845 14641 18588 7768 17917 8750 15618 18464 13925 8818 17391 13200 2575 11253 4899 12707 7147 18547 8379 17524 11779 19107 23448 10551 9856 5503 8494 1692 14681 13130 17456 15...

output:

18000
18000
21622

result:

ok 3 number(s): "18000 18000 21622"

Test #7:

score: 5
Accepted
time: 0ms
memory: 3200kb

input:

5 3
10
16030 9693 27756 24507 23211 17464 8887 7416 13135 4895
17169 11454 10400 13490 11738 22678 15050 29009 12184 12183
6893 16992 11689 16565 26352 8380 18580 20848 9410 21743
10
1128 13324 10854 697 15189 7294 1871 12509 18641 18737
8263 13121 15061 18066 16059 11121 24606 17909 1413 9992
12404...

output:

16826
16195
14672
17434
14651

result:

ok 5 number(s): "16826 16195 14672 17434 14651"

Test #8:

score: 5
Accepted
time: 3ms
memory: 5172kb

input:

10 3
50
12127 17615 4504 20695 16125 18833 13652 5613 8019 15730 16385 3568 14282 14276 13925 16974 8261 24045 9779 1487 3759 15923 7152 21549 26107 14901 14057 5048 12826 6651 12380 18877 17171 4811 18423 22750 12101 8463 7906 20052 9943 4320 11309 23876 6387 7655 3242 14459 14963 6580
20320 8545 2...

output:

18252
16219
18615
15968
20398
17291
22169
17075
17681
17390

result:

ok 10 numbers

Test #9:

score: 5
Accepted
time: 2ms
memory: 7244kb

input:

492 3
6
8092 24146 5245 25301 8773 26713
20068 26047 4375 21461 28935 4037
28742 5614 21217 11156 23813 14955
4
26596 4665 12954 28516
2539 28719 26192 23894
9531 18883 18267 9946
4
15563 2548 22395 29261
1483 598 24912 12610
17817 9629 17119 7397
5
11066 7676 29263 9309 5342
18273 24030 25203 5102 ...

output:

19776
19188
21797
18197
16488
9621
18985
14281
13947
14275
11892
9245
17832
17173
17726
9662
18620
13692
17887
16482
23818
21403
13634
12965
6193
18261
13831
13028
12865
19479
17691
16458
13698
18656
19260
6233
12146
11289
11588
13898
18827
10520
21260
13972
11976
11540
18082
16415
18846
24749
9902
...

result:

ok 492 numbers

Test #10:

score: 5
Accepted
time: 23ms
memory: 5328kb

input:

2823 3
7
11008 15379 3474 23505 21289 13545 21951
14190 11309 11018 12827 20349 17613 20448
14242 15097 16392 7823 18077 27988 12213
4
10305 19014 15560 26972
21128 9755 5367 23887
17563 15511 13210 12065
5
7016 12729 24261 5979 12169
21967 10993 25792 11527 11476
21349 2172 20428 22930 23721
6
1562...

output:

14603
13762
18256
16513
15764
21273
11977
12505
8396
21677
12517
21399
12827
15856
14628
12291
14834
18210
19601
13710
10595
10094
17897
16185
18524
13388
10652
12097
8966
11074
9961
9831
15566
19751
18182
15649
12751
15690
12487
15892
16475
15285
5549
11479
13063
8339
9186
13834
14067
12428
23249
1...

result:

ok 2823 numbers

Test #11:

score: 5
Accepted
time: 207ms
memory: 6464kb

input:

4 3
30000
8497 11286 8353 2876 13129 10774 10578 13288 16631 14892 4744 10355 11083 16465 12953 9099 9278 12561 12862 13392 1500 17077 11412 4467 14626 14772 8321 8327 3999 11277 13029 4212 1734 13474 13656 8398 762 10438 14080 15473 3866 7574 10692 16389 4413 9044 18604 1591 10649 14576 10155 10361...

output:

10103
10118
10196
10178

result:

ok 4 number(s): "10103 10118 10196 10178"

Test #12:

score: 5
Accepted
time: 263ms
memory: 7708kb

input:

3 3
50000
19199 27507 19780 17075 20200 20902 22589 12623 21750 21336 4538 24298 18509 16201 16796 6650 19468 19507 25451 27321 18009 23942 24617 24422 26676 8908 4600 11408 20067 23198 23582 16086 21592 19845 18972 17985 11853 25099 28717 27759 10596 23082 27902 19323 16693 8408 26792 20131 22735 2...

output:

10040
15453
19023

result:

ok 3 number(s): "10040 15453 19023"

Test #13:

score: 5
Accepted
time: 3ms
memory: 5284kb

input:

5 4
10
12871 9817 14695 11872 16291 27028 1392 12572 10409 3017
16310 28902 7458 24546 13098 2633 26901 14337 17441 63
26236 2514 24674 23157 9327 13531 25862 26834 24256 21476
22242 24748 23021 17878 26324 12312 3909 28308 3462 20168
10
20846 29205 14900 19250 722 6157 15661 8441 17182 11169
3390 1...

output:

24685
22656
22046
23738
22355

result:

ok 5 number(s): "24685 22656 22046 23738 22355"

Test #14:

score: 5
Accepted
time: 19ms
memory: 4860kb

input:

10 4
50
19704 7323 15828 22182 10893 7859 16948 16603 28637 8205 12361 2532 5845 23202 10647 14169 15901 24468 1029 25019 26328 18995 14057 6145 13869 15274 5361 22018 4967 1745 16752 19883 926 78 6747 7515 8391 21114 28634 18985 27576 5184 3502 25152 21983 2702 11587 9661 9052 23658
5746 9465 16337...

output:

26043
27185
27397
26352
28517
26578
25856
25676
28104
25894

result:

ok 10 numbers

Test #15:

score: 5
Accepted
time: 39ms
memory: 9484kb

input:

296 4
5
14984 9981 9017 21474 22589
15490 4551 17062 14214 18005
5994 11883 5919 13543 16909
18835 23523 17149 11464 14979
6
14169 24435 27207 2521 14770 2155
8254 9241 12877 22826 18706 14972
19413 12325 7660 19399 13082 17109
21888 14786 17154 13755 1801 9630
7
12587 18433 18081 13142 19835 14986 ...

output:

12511
10524
18268
18094
17939
12851
11657
17111
14244
19726
17196
16208
18788
15137
18399
10145
14959
13270
14484
20078
14814
18087
16894
11828
23192
24887
14794
15738
21539
22505
20498
11968
17556
21716
9821
21038
20046
13142
19941
18880
15711
10544
19569
13011
12045
18762
11510
13108
20176
16102
1...

result:

ok 296 numbers

Test #16:

score: 5
Accepted
time: 89ms
memory: 8804kb

input:

556 4
6
24538 1561 15955 25462 13820 18425
23525 16200 7272 8057 14559 13522
4043 13481 11449 26789 19442 22612
19442 17815 15054 14372 24215 15908
7
4992 21533 18540 20730 21070 25918 14971
18953 15648 18255 12969 20983 12375 20955
4379 23010 25490 28045 16038 13733 27907
7885 27116 20057 29239 136...

output:

15340
23053
13559
23172
14989
14339
12399
12797
17439
19464
17954
16936
16876
23513
15667
12337
19783
13724
18516
21357
18068
22247
12341
23511
14801
10506
11572
9374
17598
13758
13755
17614
20779
15389
17645
14177
15511
23190
20611
15695
21937
22119
13534
20474
18298
20579
14677
14012
19496
20429
1...

result:

ok 556 numbers

Test #17:

score: 5
Accepted
time: 313ms
memory: 17024kb

input:

910 4
5
18495 4213 7072 24451 5053
24983 1518 19953 25740 1087
4368 10700 8715 18200 28641
14575 2496 27461 27488 13358
4
9653 12755 22556 17654
21198 18496 17358 19398
27223 11305 27527 21344
22554 19509 8064 23976
7
1138 20361 16799 21891 18013 21953 4783
12601 3538 13276 22157 13538 10272 24627
2...

output:

21955
14468
16634
18296
16350
11548
20506
13501
22262
19344
18475
20066
17636
15124
24275
19747
21989
20782
18640
14872
11375
14879
21998
18443
13717
25115
12706
18136
23493
13745
16521
12208
16001
18263
18400
23617
10163
14338
15277
23845
14012
17613
19900
8042
0
11551
13270
15543
21395
17116
17366...

result:

ok 910 numbers

Test #18:

score: 5
Accepted
time: 605ms
memory: 26920kb

input:

1826 4
5
12038 3573 20463 6119 19486
9769 26360 9873 3880 27288
14634 18318 2181 14462 7044
18465 4615 19511 28217 543
5
29712 7598 12660 5404 13631
8149 15497 14057 12916 16856
23561 10256 5404 6465 13656
6258 12169 17157 15896 14018
6
10545 25243 9487 14739 13377 3270
7836 5934 29067 27149 19583 1...

output:

11898
14215
22984
21851
15408
19410
7192
19889
17373
22863
19336
16456
16888
13820
16097
13914
8616
10866
13444
14903
11845
17773
24730
17164
23083
18271
15779
25674
19116
24944
22504
9813
19429
14764
16853
15871
23291
16732
19851
19080
17257
13462
12114
15378
20429
18445
18873
19736
15068
13227
265...

result:

ok 1826 numbers

Test #19:

score: 5
Accepted
time: 983ms
memory: 38712kb

input:

1834 4
5
14031 15017 11718 8584 24580
2938 13523 5087 3420 27335
16189 5418 9070 14669 21628
22686 16121 15677 21857 26168
4
13628 4601 13896 2382
5508 5343 25068 22533
17027 14789 2864 19773
26676 3034 11148 26438
5
20602 13848 6353 13350 23468
26366 10523 24372 8484 24138
4007 21029 5227 14962 231...

output:

18751
17932
17785
20577
14948
18357
13497
10772
14615
10585
12000
15998
16288
15554
16718
18448
21625
12774
14554
16940
17792
13385
15268
18042
20204
13673
18532
17792
20182
12990
20140
18575
18426
19062
13277
15160
17362
23041
14704
13052
19214
9358
10732
16774
14760
19422
10394
15437
16916
20923
2...

result:

ok 1834 numbers

Test #20:

score: 5
Accepted
time: 1828ms
memory: 41592kb

input:

3 4
10000
27024 14111 22404 5025 23580 12815 13934 2337 12175 11404 6514 12919 22433 27537 25780 7967 6642 17225 21192 14830 1691 1887 11039 7793 3178 8346 22904 16381 5296 29290 20246 12878 21843 11597 11199 7069 14778 10902 24331 15641 21285 16597 16428 20494 26998 7139 14721 10644 25003 13823 268...

output:

29748
29742
29887

result:

ok 3 number(s): "29748 29742 29887"

Extra Test:

score: 0
Extra Test Passed