#include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<int> a, sa; for(int i = 0 ;i < n; i++){ int temp; cin >> temp; a.push_back(temp); sa.push_back(temp); } sort(sa.begin(), sa.end()); int p[3000]; for(int i = 0; i < n; i++) p[sa[i]-1]=i; int fin = 0; bool comp = 0; vector<int> pain; vector<int> pain2; while(!comp){ bool changed[n]; comp = 1; int ctr =0; fin++; for(int i = 0; i < n; i++) changed[i] = 1; int i, temp; for(int ii = 0; ii < n; ii++){ i=ii; starte: if(changed[i]&&changed[p[a[i]-1]]&&i!=p[a[i]-1]){ comp=0; changed[i]=0; changed[p[a[i]-1]]=0; temp=a[i]; a[i]=a[p[temp-1]]; a[p[temp-1]]=temp; pain2.push_back(i+1); pain2.push_back(p[temp-1]+1); ctr+=2; i=p[a[i]-1]; goto starte; } } if(!comp){ pain.push_back(ctr); } } cout << fin-1 << '\n'; int sum = 0; for(int i = 0; i < fin-1; i++){ cout << pain[i] << '\n'; int output[pain[i]]; int iii = 0; for(int j = sum; j < sum+pain[i]; j+=2){ output[iii]=pain2[j]; iii++; } iii=pain[i]-1; for(int j = sum+1; j < sum+pain[i]; j+=2){ output[iii]=pain2[j]; iii--; } for(int j = 0; j < pain[i]; j++) cout << output[j] << ' '; cout << '\n'; sum+=pain[i]; } 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 | #include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<int> a, sa; for(int i = 0 ;i < n; i++){ int temp; cin >> temp; a.push_back(temp); sa.push_back(temp); } sort(sa.begin(), sa.end()); int p[3000]; for(int i = 0; i < n; i++) p[sa[i]-1]=i; int fin = 0; bool comp = 0; vector<int> pain; vector<int> pain2; while(!comp){ bool changed[n]; comp = 1; int ctr =0; fin++; for(int i = 0; i < n; i++) changed[i] = 1; int i, temp; for(int ii = 0; ii < n; ii++){ i=ii; starte: if(changed[i]&&changed[p[a[i]-1]]&&i!=p[a[i]-1]){ comp=0; changed[i]=0; changed[p[a[i]-1]]=0; temp=a[i]; a[i]=a[p[temp-1]]; a[p[temp-1]]=temp; pain2.push_back(i+1); pain2.push_back(p[temp-1]+1); ctr+=2; i=p[a[i]-1]; goto starte; } } if(!comp){ pain.push_back(ctr); } } cout << fin-1 << '\n'; int sum = 0; for(int i = 0; i < fin-1; i++){ cout << pain[i] << '\n'; int output[pain[i]]; int iii = 0; for(int j = sum; j < sum+pain[i]; j+=2){ output[iii]=pain2[j]; iii++; } iii=pain[i]-1; for(int j = sum+1; j < sum+pain[i]; j+=2){ output[iii]=pain2[j]; iii--; } for(int j = 0; j < pain[i]; j++) cout << output[j] << ' '; cout << '\n'; sum+=pain[i]; } return 0; } |