QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#934986#10214. Sort and \&2147483647 (Yuyang Chen, Zongrun Ouyang, Xiaoyi Liu)#AC ✓18ms4020kbC++142.2kb2025-03-15 11:22:462025-03-15 11:22:56

Judging History

This is the latest submission verdict.

  • [2025-03-15 11:22:56]
  • Judged
  • Verdict: AC
  • Time: 18ms
  • Memory: 4020kb
  • [2025-03-15 11:22:46]
  • Submitted

answer

#include<bits/stdc++.h>

#define MAXN 10005

int n,C=1,D=0;
std::vector<std::pair<int,int> > ans;
int a[MAXN];
std::vector<int> tmp;
int vis[MAXN];

void swp(int x,int y){
    if(x==y) return;
    if(x&y){
        for(int i=0;i<D;i++){
            if(!(C>>i)) break;
            if((1^(x>>i)&1)&&(1^(y>>i)&1)){
                int t=(1<<i);
                ans.push_back(std::make_pair(t,x));
                ans.push_back(std::make_pair(t,y));
                ans.push_back(std::make_pair(t,x));
                return (void)(std::swap(a[x],a[y]));
            }
        }    
    }
    ans.push_back(std::make_pair(x,y));
    std::swap(a[x],a[y]);
    return;
}

void run(){
    std::cin>>n;
    for(int i=1;i<=n;i++) std::cin>>a[i];
    for(int i=1;i<=n;i++) vis[i]=0;
    ans.clear();
    if(n==1){
        std::cout<<0<<'\n';
        std::cout<<0<<'\n';
        return;
    }else if(n==2){
        if(a[1]==1) std::cout<<0<<'\n'<<0<<'\n';
        else{
            std::cout<<0<<'\n'<<1<<'\n'<<"1 2"<<'\n';
        }
        return;
    }
    C=1,D=0;
    while(C<=n) C*=2,++D;
    std::cout<<(n==C-1&&a[n]!=n)<<'\n';
    for(int i=1;i<=n;i++){
        if(vis[i]) continue;
        int now=i;
        tmp.clear();
        while(!vis[now]){
            tmp.push_back(now);
            vis[now]=1;
            now=a[now];
        }
        if(tmp.size()==1) continue;
        bool hay=false;
        for(int j:tmp) hay|=(j==(C-1));
        if(hay){
            for(std::vector<int>::iterator it=tmp.begin();it!=tmp.end();++it){
                if(*it==C-1){
                    tmp.erase(it);
                    break;
                }
            }
        }
        for(int j=0;j<=tmp.size();j++){
            swp(1,tmp[j%tmp.size()]); 
        }
    }
    
    if(n==C-1&&a[n]!=n){
        int pos=a[n];
        swp(pos,1);
        swp(1,n); 
        swp(1,pos);       
    }

    std::cout<<ans.size()<<'\n';
    for(std::pair<int,int> x:ans){
        std::cout<<x.first<<' '<<x.second<<'\n';
    }

    return;
}

int T;
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),std::cout.tie(0); 

    std::cin>>T;
    for(int i=1;i<=T;i++){
        run();
    }

    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3712kb

input:

1
4
3 1 4 2

output:

0
5
4 1
4 3
4 1
1 4
1 2

result:

ok all sorted (1 test case)

Test #2:

score: 0
Accepted
time: 18ms
memory: 3712kb

input:

247
31
13 2 10 25 1 28 26 22 30 16 19 29 15 14 24 17 27 4 21 18 9 20 23 11 31 8 7 12 6 5 3
339
214 112 269 250 173 209 235 27 176 239 139 60 251 65 253 230 291 135 191 298 249 23 151 11 45 88 302 83 183 297 317 100 325 172 38 307 182 98 12 70 207 91 130 193 137 243 32 160 285 185 64 199 202 190 211 ...

output:

1
63
2 1
2 13
2 1
16 1
16 15
16 1
1 24
4 1
4 11
4 1
4 1
4 19
4 1
2 1
2 21
2 1
2 1
2 9
2 1
1 30
2 1
2 5
2 1
4 1
4 3
4 1
1 10
1 16
2 1
2 17
2 1
4 1
4 27
4 1
8 1
8 7
8 1
1 26
1 8
1 22
1 20
1 18
1 4
2 1
2 25
2 1
4 1
4 3
4 1
1 6
1 28
1 12
2 1
2 29
2 1
1 6
4 3
4 1
4 3
1 31
4 1
4 3
4 1
0
683
1 214
1 196
1 ...

result:

ok all sorted (247 test cases)

Test #3:

score: 0
Accepted
time: 17ms
memory: 3892kb

input:

31
2897
687 310 1393 2586 1050 2751 2328 2223 638 2827 2493 1613 1034 1724 1579 2819 1761 2087 1213 2717 1884 2364 2396 2106 1722 8 2491 473 978 1274 403 2325 1711 156 1227 2696 2069 2419 2472 1787 2487 2761 2822 2505 1739 2307 507 2128 1804 1079 199 347 1462 1891 198 583 2201 1399 940 1958 1830 318...

output:

0
5807
16 1
16 687
16 1
4 1
4 1123
4 1
1 1560
1 668
1 2820
1 2794
1 552
1 1790
1 1530
128 1
128 1151
128 1
4 1
4 2091
4 1
1 1118
1 2318
1 1206
1 206
8 1
8 2391
8 1
4 1
4 131
4 1
1 1638
2 1
2 1493
2 1
2 1
2 1957
2 1
2 1
2 2777
2 1
2 1
2 829
2 1
1 2036
4 1
4 2675
4 1
2 1
2 2797
2 1
4 1
4 211
4 1
2 1
2...

result:

ok all sorted (31 test cases)

Test #4:

score: 0
Accepted
time: 17ms
memory: 4020kb

input:

12
7
7 5 3 6 1 4 2
5
5 3 4 1 2
10000
1132 8176 3778 365 3479 4419 5652 7687 7968 2419 81 6716 4933 9926 3015 5642 6300 5227 5671 678 5590 5495 3467 8806 6977 3273 8322 285 2531 4663 4111 8107 7725 9352 1799 4423 958 5421 7445 6745 1769 9250 7262 9443 7285 3548 573 9838 4609 8840 7834 9799 7356 5748 ...

output:

1
10
1 2
2 1
2 5
2 1
1 4
1 6
1 4
2 1
1 7
1 2
0
8
2 1
2 5
2 1
1 2
4 1
4 3
4 1
1 4
0
20012
1 1132
1 8500
1 5750
1 6320
1 364
1 4662
1 4442
1 3836
2 1
2 7633
2 1
1 6638
1 2470
1 3098
1 8192
8 1
8 3991
8 1
1 3644
1 3624
1 5338
2 1
2 1525
2 1
8 1
8 8727
8 1
32 1
32 2463
32 1
1 3928
2 1
2 8769
2 1
16 1
16...

result:

ok all sorted (12 test cases)

Extra Test:

score: 0
Extra Test Passed