#include <iostream> #include <algorithm> using namespace std; int main() { int n; int it; cin >> n; int wzr[n]; int temp[n][n+1]; int T=0; int k; int sorted[n]; for(int i=0;i<n;i++) { cin >> wzr[i]; } for(int i=0;i<n;i++) { sorted[i] = wzr[i]; } sort(sorted,sorted+n); for(int i=0;i<n;i++) { wzr[i] = distance(sorted,find(sorted,sorted+n,wzr[i]))+1; } for(int i=0;i<n;i++) { sorted[i] = wzr[i]; } int c=0; while (c<n) { it=1; k=0; c=0; while(it<n) { if(sorted[it-1]!=it && distance(sorted,find(sorted,sorted+n,it))>it-1) { temp[T][k] = it; temp[T][k+1] = distance(sorted,find(sorted,sorted+n,it))+1; k = k+2; swap(wzr[it-1],wzr[distance(sorted,find(sorted,sorted+n,it))]); } it++; } for(int i=k;i<n+1;i++) { temp[T][i] = 0; } T++; for(int i=0;i<n;i++) { sorted[i] = wzr[i]; if(wzr[i]==i+1) { c++; } } } cout << T << endl; for (int j=0;j<T;j++) { k=0; while(temp[j][k]!=0) { k++; } cout << k << endl; for(int i=0;i<=k-2;i+=2) { cout << temp[j][i] << " "; } for(int i=k-1;i>=1;i-=2) { cout << temp[j][i] << " "; } cout << endl; } }
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 | #include <iostream> #include <algorithm> using namespace std; int main() { int n; int it; cin >> n; int wzr[n]; int temp[n][n+1]; int T=0; int k; int sorted[n]; for(int i=0;i<n;i++) { cin >> wzr[i]; } for(int i=0;i<n;i++) { sorted[i] = wzr[i]; } sort(sorted,sorted+n); for(int i=0;i<n;i++) { wzr[i] = distance(sorted,find(sorted,sorted+n,wzr[i]))+1; } for(int i=0;i<n;i++) { sorted[i] = wzr[i]; } int c=0; while (c<n) { it=1; k=0; c=0; while(it<n) { if(sorted[it-1]!=it && distance(sorted,find(sorted,sorted+n,it))>it-1) { temp[T][k] = it; temp[T][k+1] = distance(sorted,find(sorted,sorted+n,it))+1; k = k+2; swap(wzr[it-1],wzr[distance(sorted,find(sorted,sorted+n,it))]); } it++; } for(int i=k;i<n+1;i++) { temp[T][i] = 0; } T++; for(int i=0;i<n;i++) { sorted[i] = wzr[i]; if(wzr[i]==i+1) { c++; } } } cout << T << endl; for (int j=0;j<T;j++) { k=0; while(temp[j][k]!=0) { k++; } cout << k << endl; for(int i=0;i<=k-2;i+=2) { cout << temp[j][i] << " "; } for(int i=k-1;i>=1;i-=2) { cout << temp[j][i] << " "; } cout << endl; } } |