#include<bits/stdc++.h> #define random randomA typedef long long ll; using namespace std; const int N=3000010; int p[N]; int h[N]; vector<vector<int> >ans; int n; bool used[N]; void go(){ for (int i=1;i<=n;i++){ used[i]=false; } vector<int>A,B; for (int i=1;i<=n;i++){ if (used[i]) continue; int x=i; vector<int>cyc; while (!used[x]){ cyc.push_back(x); used[x]=true; x=p[x]; } for (int i=0;i<(int)cyc.size()-i-1;i++){ A.push_back(cyc[i]); B.push_back(cyc[(int)cyc.size()-i-1]); } } vector<int>v=A; reverse(B.begin(),B.end()); for (int i:B) v.push_back(i); vector<int>val; vector<int>val1; for (int i:v) val.push_back(p[i]); for (int i:v) val1.push_back(h[i]); reverse(val.begin(),val.end()); reverse(val1.begin(),val1.end()); for (int i=0;i<val.size();i++) p[v[i]]=val[i]; for (int i=0;i<val1.size();i++) h[v[i]]=val1[i]; ans.push_back(v); } void solve(){ cin>>n; vector<pair<int,int> >v; for (int i=1;i<=n;i++) cin>>h[i]; // for (int i=1;i<=n;i++) h[i]=i; // random_shuffle(h+1,h+n+1); for (int i=1;i<=n;i++){ v.push_back({h[i],i}); } sort(v.begin(),v.end()); for (int i=0;i<v.size();i++){ p[v[i].second]=i+1; } while (!is_sorted(p+1,p+n+1)){ go(); // for (int i=1;i<=n;i++) cout<<p[i]<<" "; // cout<<endl; } if (!is_sorted(h+1,h+n+1)) exit(1); cout<<ans.size()<<'\n'; for (auto v:ans){ cout<<v.size()<<'\n'; for (int i:v) cout<<i<<" "; cout<<'\n'; } ans.clear(); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int tt=1; while (tt--){ solve(); } return 0; } /* 10 2 3 4 5 6 7 8 9 10 1 */
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 | #include<bits/stdc++.h> #define random randomA typedef long long ll; using namespace std; const int N=3000010; int p[N]; int h[N]; vector<vector<int> >ans; int n; bool used[N]; void go(){ for (int i=1;i<=n;i++){ used[i]=false; } vector<int>A,B; for (int i=1;i<=n;i++){ if (used[i]) continue; int x=i; vector<int>cyc; while (!used[x]){ cyc.push_back(x); used[x]=true; x=p[x]; } for (int i=0;i<(int)cyc.size()-i-1;i++){ A.push_back(cyc[i]); B.push_back(cyc[(int)cyc.size()-i-1]); } } vector<int>v=A; reverse(B.begin(),B.end()); for (int i:B) v.push_back(i); vector<int>val; vector<int>val1; for (int i:v) val.push_back(p[i]); for (int i:v) val1.push_back(h[i]); reverse(val.begin(),val.end()); reverse(val1.begin(),val1.end()); for (int i=0;i<val.size();i++) p[v[i]]=val[i]; for (int i=0;i<val1.size();i++) h[v[i]]=val1[i]; ans.push_back(v); } void solve(){ cin>>n; vector<pair<int,int> >v; for (int i=1;i<=n;i++) cin>>h[i]; // for (int i=1;i<=n;i++) h[i]=i; // random_shuffle(h+1,h+n+1); for (int i=1;i<=n;i++){ v.push_back({h[i],i}); } sort(v.begin(),v.end()); for (int i=0;i<v.size();i++){ p[v[i].second]=i+1; } while (!is_sorted(p+1,p+n+1)){ go(); // for (int i=1;i<=n;i++) cout<<p[i]<<" "; // cout<<endl; } if (!is_sorted(h+1,h+n+1)) exit(1); cout<<ans.size()<<'\n'; for (auto v:ans){ cout<<v.size()<<'\n'; for (int i:v) cout<<i<<" "; cout<<'\n'; } ans.clear(); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int tt=1; while (tt--){ solve(); } return 0; } /* 10 2 3 4 5 6 7 8 9 10 1 */ |