Submission #760786

# Submission time Handle Problem Language Result Execution time Memory
760786 2023-06-18T12:51:56 Z bachhoangxuan Xor Sort (eJOI20_xorsort) C++17
100 / 100
8 ms 1044 KB
#include <bits/stdc++.h>
using namespace std;
#define maxn 1005
#define pii pair<int,int>
vector<pii> ans;
int a[maxn],n;
void solve1(){
    vector<int> p;
    for(int i=1;i<=n;i++) p.push_back(a[i]);
    sort(p.begin(),p.end());
    for(int i=1;i<=n;i++) a[i]=lower_bound(p.begin(),p.end(),a[i])-p.begin()+1;
    for(int i=n;i>=2;i--) ans.push_back({i,i-1});
    for(int i=1;i<n;i++){
        int id=-1;
        for(int j=i;j<=n;j++){
            if(a[j]==i) id=j;
        }
        while(id!=i){
            if(id!=n) ans.push_back({id+1,id});
            ans.push_back({id-1,id});swap(a[id-1],a[id]);
            id--;
        }
        ans.push_back({i+1,i});
    }
}
void solve2(){
    for(int i=19;i>=0;i--){
        bool c=false;
        for(int j=1;j<=n;j++){
            if(a[j]>=(1<<(i+1))) break;
            if(c){
                if(a[j]&(1<<i)){ans.push_back({j-1,j});a[j-1]^=a[j];}
                else{
                    ans.push_back({j,j-1});ans.push_back({j-1,j});
                    a[j]^=a[j-1];a[j-1]^=a[j];
                }
            }
            else if(a[j]&(1<<i)) c=true;
        }
    }
}
int main(){
    //freopen("XORSORT.INP","r",stdin);
    //freopen("XORSORT.OUT","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int s;cin >> n >> s;
    for(int i=1;i<=n;i++) cin >> a[i];
    if(s==1) solve1();
    else solve2();
    cout << (int)ans.size() << '\n';
    for(pii x:ans) cout << x.first << ' ' << x.second << '\n';
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 2 ms 576 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 3 ms 728 KB Output is correct
13 Correct 3 ms 728 KB Output is correct
14 Correct 3 ms 728 KB Output is correct
15 Correct 3 ms 712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 2 ms 576 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 3 ms 728 KB Output is correct
13 Correct 3 ms 728 KB Output is correct
14 Correct 3 ms 728 KB Output is correct
15 Correct 3 ms 712 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 2 ms 576 KB Output is correct
18 Correct 3 ms 728 KB Output is correct
19 Correct 3 ms 728 KB Output is correct
20 Correct 3 ms 728 KB Output is correct
21 Correct 3 ms 728 KB Output is correct
22 Correct 3 ms 728 KB Output is correct
23 Correct 3 ms 728 KB Output is correct
24 Correct 3 ms 728 KB Output is correct
25 Correct 3 ms 728 KB Output is correct
26 Correct 5 ms 1044 KB Output is correct
27 Correct 5 ms 980 KB Output is correct
28 Correct 5 ms 968 KB Output is correct
29 Correct 8 ms 1028 KB Output is correct
30 Correct 5 ms 968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 4 ms 856 KB Output is correct
6 Correct 4 ms 840 KB Output is correct
7 Correct 4 ms 856 KB Output is correct
8 Correct 4 ms 856 KB Output is correct
9 Correct 4 ms 844 KB Output is correct
10 Correct 4 ms 844 KB Output is correct
11 Correct 4 ms 856 KB Output is correct
12 Correct 4 ms 884 KB Output is correct
13 Correct 4 ms 856 KB Output is correct
14 Correct 4 ms 976 KB Output is correct
15 Correct 5 ms 968 KB Output is correct