#include <bits/stdc++.h> using namespace std; const int N = 3007; int A[N],P[N],vis[N],B[N]; void swp(int a,int b){ P[A[a]] = b; P[A[b]] = a; swap(A[a],A[b]); } void solve(){ int n; cin>>n; set<int> S; for(int i = 1;i<=n;i+=1){ cin>>A[i]; // A[i] = i; S.insert(A[i]); vis[i] = 0; } // random_shuffle(A+1,A+1+n); int ptr = 0; map<int,int> M; for(int to:S){ M[to] = ++ptr; } /*for(int i = 1;i<=n;i+=1){ A[i] = i; } random_shuffle(A+1,A+1+n);*/ for(int i = 1;i<=n;i+=1){ A[i] = M[A[i]]; P[A[i]] = i; B[i] = A[i]; // cout<<A[i]<<' '; } // cout<<endl; vector<vector<pair<int,int> > > ans; int flag = 0; for(int i = 1;i<n;i+=1){ if (A[i]>A[i+1]){ flag = 1; break; } } if (flag){ flag = 0; for(int i = 1;i<=n;i+=1){ if (P[P[i]]!=i){ flag = 1; break; } } if (flag){ ans.resize(2); for(int i = 1;i<=n;i+=1){ if (!vis[i]){ vector<int> seq; int x = i; do{ vis[x] = 1; seq.push_back(x); x = A[x]; }while(x!=i); int sz = seq.size(); // cout<<"GDB "<<sz<<endl; // for(int to:seq){ // cout<<to<<' '; // } // cout<<endl; for(int i = 1;i<sz-i;i+=1){ ans[0].push_back({P[seq[i]],P[seq[sz-i]]}); swp(P[seq[i]],P[seq[sz-i]]); } } } } else{ ans.resize(1); } for(int i = 1;i<=n;i+=1){ if (A[i]!=i){ int pos = P[i]; ans.back().push_back({i,pos}); swp(i,pos); } } } for(int i = 1;i<n;i+=1){ if (A[i]>A[i+1]){ assert(0); } } if (ans.size()>2){ for(int i = 1;i<=n;i+=1){ cout<<B[i]<<' '; } cout<<endl; exit(0); } else{ } cout<<ans.size()<<endl; for(auto &V:ans){ int sz = V.size(); cout<<sz*2<<endl; for(int c = 0;c<2;c+=1){ for(auto &to:V){ cout<<to.first<<' '; swap(to.first,to.second); } reverse(V.begin(),V.end()); } cout<<endl; } } signed main(){ int tt = 1; while(tt--){ solve(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #include <bits/stdc++.h> using namespace std; const int N = 3007; int A[N],P[N],vis[N],B[N]; void swp(int a,int b){ P[A[a]] = b; P[A[b]] = a; swap(A[a],A[b]); } void solve(){ int n; cin>>n; set<int> S; for(int i = 1;i<=n;i+=1){ cin>>A[i]; // A[i] = i; S.insert(A[i]); vis[i] = 0; } // random_shuffle(A+1,A+1+n); int ptr = 0; map<int,int> M; for(int to:S){ M[to] = ++ptr; } /*for(int i = 1;i<=n;i+=1){ A[i] = i; } random_shuffle(A+1,A+1+n);*/ for(int i = 1;i<=n;i+=1){ A[i] = M[A[i]]; P[A[i]] = i; B[i] = A[i]; // cout<<A[i]<<' '; } // cout<<endl; vector<vector<pair<int,int> > > ans; int flag = 0; for(int i = 1;i<n;i+=1){ if (A[i]>A[i+1]){ flag = 1; break; } } if (flag){ flag = 0; for(int i = 1;i<=n;i+=1){ if (P[P[i]]!=i){ flag = 1; break; } } if (flag){ ans.resize(2); for(int i = 1;i<=n;i+=1){ if (!vis[i]){ vector<int> seq; int x = i; do{ vis[x] = 1; seq.push_back(x); x = A[x]; }while(x!=i); int sz = seq.size(); // cout<<"GDB "<<sz<<endl; // for(int to:seq){ // cout<<to<<' '; // } // cout<<endl; for(int i = 1;i<sz-i;i+=1){ ans[0].push_back({P[seq[i]],P[seq[sz-i]]}); swp(P[seq[i]],P[seq[sz-i]]); } } } } else{ ans.resize(1); } for(int i = 1;i<=n;i+=1){ if (A[i]!=i){ int pos = P[i]; ans.back().push_back({i,pos}); swp(i,pos); } } } for(int i = 1;i<n;i+=1){ if (A[i]>A[i+1]){ assert(0); } } if (ans.size()>2){ for(int i = 1;i<=n;i+=1){ cout<<B[i]<<' '; } cout<<endl; exit(0); } else{ } cout<<ans.size()<<endl; for(auto &V:ans){ int sz = V.size(); cout<<sz*2<<endl; for(int c = 0;c<2;c+=1){ for(auto &to:V){ cout<<to.first<<' '; swap(to.first,to.second); } reverse(V.begin(),V.end()); } cout<<endl; } } signed main(){ int tt = 1; while(tt--){ solve(); } } |