#include <iostream> #include <vector> #include <algorithm> using namespace std; const int N = 3000+2; int a[N]; int b[N]; int c[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for(int i = 0; i < n; ++i) { cin >> a[i]; b[i] = a[i]; c[a[i]] = i; } sort(b, b+n); for(int i = 0; i < n; ++i) { a[c[b[i]]] = i; } // for(int i = 0; i < n; ++i) { // cout << a[i] << ' '; // } // cout << endl; vector< vector<int> > v; while(1) { // for(int i = 0; i < n; ++i) { // cout << a[i] << ' '; // } // cout << endl; bool sorted = true; for(int i = 0; i < n; ++i) { if(a[i] != i) { sorted = false; break; } } if(sorted) break; vector<int> vvl, vvr; for(int i = 0; i < n; ++i) b[i] = 0; for(int i = 0; i < n; ++i) { if(a[i] == i || b[i] > 0) continue; if(i == a[a[i]]) { b[i] = 1; b[a[i]] = 1; vvl.push_back(i); vvr.push_back(a[i]); swap(a[i], a[a[i]]); continue; } int ct = i; b[ct] = 1; while(a[ct] != i && a[a[ct]] != i) { // swap ct and a[ct] vvl.push_back(ct); vvr.push_back(a[ct]); b[a[ct]] = 1; b[a[a[ct]]] = 1; int tct = a[a[ct]]; swap(a[ct], a[a[ct]]); ct = tct; } } reverse(vvr.begin(), vvr.end()); vvl.insert(vvl.end(), vvr.begin(), vvr.end()); v.push_back(vvl); } cout << v.size() << '\n'; for(int i = 0; i < v.size(); ++i) { cout << v[i].size() << '\n'; for(int j = 0; j < v[i].size(); ++j) { cout << v[i][j]+1 << ' '; } 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 100 101 102 103 104 105 | #include <iostream> #include <vector> #include <algorithm> using namespace std; const int N = 3000+2; int a[N]; int b[N]; int c[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for(int i = 0; i < n; ++i) { cin >> a[i]; b[i] = a[i]; c[a[i]] = i; } sort(b, b+n); for(int i = 0; i < n; ++i) { a[c[b[i]]] = i; } // for(int i = 0; i < n; ++i) { // cout << a[i] << ' '; // } // cout << endl; vector< vector<int> > v; while(1) { // for(int i = 0; i < n; ++i) { // cout << a[i] << ' '; // } // cout << endl; bool sorted = true; for(int i = 0; i < n; ++i) { if(a[i] != i) { sorted = false; break; } } if(sorted) break; vector<int> vvl, vvr; for(int i = 0; i < n; ++i) b[i] = 0; for(int i = 0; i < n; ++i) { if(a[i] == i || b[i] > 0) continue; if(i == a[a[i]]) { b[i] = 1; b[a[i]] = 1; vvl.push_back(i); vvr.push_back(a[i]); swap(a[i], a[a[i]]); continue; } int ct = i; b[ct] = 1; while(a[ct] != i && a[a[ct]] != i) { // swap ct and a[ct] vvl.push_back(ct); vvr.push_back(a[ct]); b[a[ct]] = 1; b[a[a[ct]]] = 1; int tct = a[a[ct]]; swap(a[ct], a[a[ct]]); ct = tct; } } reverse(vvr.begin(), vvr.end()); vvl.insert(vvl.end(), vvr.begin(), vvr.end()); v.push_back(vvl); } cout << v.size() << '\n'; for(int i = 0; i < v.size(); ++i) { cout << v[i].size() << '\n'; for(int j = 0; j < v[i].size(); ++j) { cout << v[i][j]+1 << ' '; } cout << '\n'; } return 0; } |