#include <iostream> #include <stdio.h> #include <vector> #include <algorithm> using namespace std; int n,res; int a[3001],h[3001]; int v[3001]; vector<int> r[4],c; void dfs(int i) { v[i]=res; c.push_back(i); if(v[a[i]]<res) dfs(a[i]); } int main() { scanf("%d",&n); //cout<<n<<endl; for(int i=1;i<=n;i++) { scanf("%d",&h[i]); //cout<<h[i]<<" "; h[i]=(h[i]<<15)+i; } //cout<<endl; sort(h+1,h+n+1); for(int i=1;i<=n;i++) a[h[i]&((1<<15)-1)]=i; while(true) { res++; for(int i=1;i<=n;i++) { if(v[i]<res&&a[i]!=i) { c.clear(); dfs(i); for(int j=0;j*2<c.size()-1;j++) { r[res].push_back(c[j]); r[res].push_back(c[c.size()-j-1]); swap(a[c[j]],a[c[c.size()-j-1]]); } } } if(r[res].size()==0) break; } printf("%d\n",res-1); for(int i=1;i<res;i++) { printf("%d\n",r[i].size()); for(int j=0;j<r[i].size();j+=2) printf("%d ",r[i][j]); for(int j=r[i].size()-1;j>0;j-=2) printf("%d ",r[i][j]); printf("\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 | #include <iostream> #include <stdio.h> #include <vector> #include <algorithm> using namespace std; int n,res; int a[3001],h[3001]; int v[3001]; vector<int> r[4],c; void dfs(int i) { v[i]=res; c.push_back(i); if(v[a[i]]<res) dfs(a[i]); } int main() { scanf("%d",&n); //cout<<n<<endl; for(int i=1;i<=n;i++) { scanf("%d",&h[i]); //cout<<h[i]<<" "; h[i]=(h[i]<<15)+i; } //cout<<endl; sort(h+1,h+n+1); for(int i=1;i<=n;i++) a[h[i]&((1<<15)-1)]=i; while(true) { res++; for(int i=1;i<=n;i++) { if(v[i]<res&&a[i]!=i) { c.clear(); dfs(i); for(int j=0;j*2<c.size()-1;j++) { r[res].push_back(c[j]); r[res].push_back(c[c.size()-j-1]); swap(a[c[j]],a[c[c.size()-j-1]]); } } } if(r[res].size()==0) break; } printf("%d\n",res-1); for(int i=1;i<res;i++) { printf("%d\n",r[i].size()); for(int j=0;j<r[i].size();j+=2) printf("%d ",r[i][j]); for(int j=r[i].size()-1;j>0;j-=2) printf("%d ",r[i][j]); printf("\n"); } return 0; } |