#include <iostream> #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int *idsort; int *t; pair<int,int> *k; bool *p; bool spr; int n; int najnieust; int wynik=0; vector<int> V1[3003]; vector<int> V2[3003]; cin>>n; najnieust=n; idsort=new int[n+1]; p=new bool[n+1]; t=new int[n+1]; k=new pair<int,int>[n+1]; for(int i=1;i<=n;++i) { cin>>t[i]; k[i].first=t[i]; k[i].second=i; } sort(k+1,k+n+1); for(int i=1;i<=n;++i) { for(int j=1;j<=n;++j) { if(t[i]==k[j].first) { idsort[i]=j; break; } } } for(int i=0;i<n;++i) { for(int j=0;j<=n;++j) { p[j]=0; } spr=0; for(int j=n;j>=1;--j) { //cout<<"j: "<<j<<' '<<k[j].second<<' '; if(k[j].second!=j) { najnieust=j; spr=1; break; } }if(!spr) { break; } wynik++; for(int j=najnieust;j>=1;--j) { if((!p[k[j].second])&&(!p[j])) { if(k[j].second!=j) { p[j]=1; p[k[j].second]=1; V1[i].push_back(j); V2[i].push_back(k[j].second); swap(t[j],t[k[j].second]); k[idsort[j]].second=k[j].second; swap(idsort[j],idsort[k[j].second]); k[j].second=j; //cout<<"zm: "<<k[j].second<<' '<<j<<'\n'; } } } //cout<<'\n'; /*cout<<"t:\n"; for(int i=1;i<=n;++i) { cout<<t[i]<<' '; } cout<<'\n';*/ } cout<<wynik<<'\n'; for(int i=0;i<wynik;++i) { cout<<V1[i].size()+V2[i].size()<<'\n'; for(int j=0;j<(int)V1[i].size();++j) { cout<<V1[i][j]<<' '; } for(int j=V2[i].size()-1;j>=0;--j) { cout<<V2[i][j]<<' '; } cout<<'\n'; } delete [] t; delete [] k; delete [] p; delete [] idsort; 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | #include <iostream> #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int *idsort; int *t; pair<int,int> *k; bool *p; bool spr; int n; int najnieust; int wynik=0; vector<int> V1[3003]; vector<int> V2[3003]; cin>>n; najnieust=n; idsort=new int[n+1]; p=new bool[n+1]; t=new int[n+1]; k=new pair<int,int>[n+1]; for(int i=1;i<=n;++i) { cin>>t[i]; k[i].first=t[i]; k[i].second=i; } sort(k+1,k+n+1); for(int i=1;i<=n;++i) { for(int j=1;j<=n;++j) { if(t[i]==k[j].first) { idsort[i]=j; break; } } } for(int i=0;i<n;++i) { for(int j=0;j<=n;++j) { p[j]=0; } spr=0; for(int j=n;j>=1;--j) { //cout<<"j: "<<j<<' '<<k[j].second<<' '; if(k[j].second!=j) { najnieust=j; spr=1; break; } }if(!spr) { break; } wynik++; for(int j=najnieust;j>=1;--j) { if((!p[k[j].second])&&(!p[j])) { if(k[j].second!=j) { p[j]=1; p[k[j].second]=1; V1[i].push_back(j); V2[i].push_back(k[j].second); swap(t[j],t[k[j].second]); k[idsort[j]].second=k[j].second; swap(idsort[j],idsort[k[j].second]); k[j].second=j; //cout<<"zm: "<<k[j].second<<' '<<j<<'\n'; } } } //cout<<'\n'; /*cout<<"t:\n"; for(int i=1;i<=n;++i) { cout<<t[i]<<' '; } cout<<'\n';*/ } cout<<wynik<<'\n'; for(int i=0;i<wynik;++i) { cout<<V1[i].size()+V2[i].size()<<'\n'; for(int j=0;j<(int)V1[i].size();++j) { cout<<V1[i][j]<<' '; } for(int j=V2[i].size()-1;j>=0;--j) { cout<<V2[i][j]<<' '; } cout<<'\n'; } delete [] t; delete [] k; delete [] p; delete [] idsort; return 0; } |