#include <bits/stdc++.h> using namespace std; typedef long long LL; int n; int t[3003];//input int d[3003];//posortowane int p[3003];//powinno int g[3003];//gdzie sie znajduje bool used[3003]; vector< vector<int> >res; vector<int> res1, res2; void ff(int i){ if(used[p[t[i]]]==true || used[g[d[i]]]==true){ return; } used[p[t[i]]]=true; used[g[d[i]]]=true; if(t[p[t[i]]] == d[i]){ return; } res1.push_back(p[t[i]]); res2.push_back(g[d[i]]); int next_i = g[d[i]]; swap(t[p[t[i]]], t[g[d[i]]]); swap(g[t[p[t[i]]]], g[t[g[d[i]]]]); ff(next_i); } void f(){ for(int i=1; i<=n; i++){ if(i != p[t[i]] && used[i]==false){ used[i]=true; if(used[p[t[i]]]==true){ continue; } used[p[t[i]]]=true; res1.push_back(i); res2.push_back(p[t[i]]); swap(t[i], t[p[t[i]]]); swap(g[t[i]], g[t[p[t[i]]]]); if(d[i] != t[p[t[i]]]){ ff(i); }else{ used[p[t[i]]]=true; } } } for(int i=res2.size()-1; i>=0; i--){ res1.push_back(res2[i]); } res.push_back(res1); res2.clear(); res1.clear(); for(int i=0; i<=n; i++){ used[i]=false; } } bool check(){ for(int i=0; i<=n; i++){ if(t[i]!=d[i]) return false; } return true; } int main(){ ios_base::sync_with_stdio(false); cin>>n; for(int i=1; i<=n; i++){ cin>>t[i]; d[i]=t[i]; } sort(d, d+n+1); for(int i=1; i<=n; i++){ p[d[i]]=i; g[t[i]]=i; } while(!check()){ f(); } cout<<res.size()<<"\n"; for(auto v : res){ cout<<v.size()<<"\n"; for(auto c : v){ cout<<c<<" "; } cout<<"\n"; } return 0; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; int n; int t[3003];//input int d[3003];//posortowane int p[3003];//powinno int g[3003];//gdzie sie znajduje bool used[3003]; vector< vector<int> >res; vector<int> res1, res2; void ff(int i){ if(used[p[t[i]]]==true || used[g[d[i]]]==true){ return; } used[p[t[i]]]=true; used[g[d[i]]]=true; if(t[p[t[i]]] == d[i]){ return; } res1.push_back(p[t[i]]); res2.push_back(g[d[i]]); int next_i = g[d[i]]; swap(t[p[t[i]]], t[g[d[i]]]); swap(g[t[p[t[i]]]], g[t[g[d[i]]]]); ff(next_i); } void f(){ for(int i=1; i<=n; i++){ if(i != p[t[i]] && used[i]==false){ used[i]=true; if(used[p[t[i]]]==true){ continue; } used[p[t[i]]]=true; res1.push_back(i); res2.push_back(p[t[i]]); swap(t[i], t[p[t[i]]]); swap(g[t[i]], g[t[p[t[i]]]]); if(d[i] != t[p[t[i]]]){ ff(i); }else{ used[p[t[i]]]=true; } } } for(int i=res2.size()-1; i>=0; i--){ res1.push_back(res2[i]); } res.push_back(res1); res2.clear(); res1.clear(); for(int i=0; i<=n; i++){ used[i]=false; } } bool check(){ for(int i=0; i<=n; i++){ if(t[i]!=d[i]) return false; } return true; } int main(){ ios_base::sync_with_stdio(false); cin>>n; for(int i=1; i<=n; i++){ cin>>t[i]; d[i]=t[i]; } sort(d, d+n+1); for(int i=1; i<=n; i++){ p[d[i]]=i; g[t[i]]=i; } while(!check()){ f(); } cout<<res.size()<<"\n"; for(auto v : res){ cout<<v.size()<<"\n"; for(auto c : v){ cout<<c<<" "; } cout<<"\n"; } return 0; } |