Submission #760786

#TimeUsernameProblemLanguageResultExecution timeMemory
760786bachhoangxuanXor Sort (eJOI20_xorsort)C++17
100 / 100
8 ms1044 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...