#include <bits/stdc++.h> using namespace std; int h[3003], h2[3003], odw[3003]; int pos1[3003], pos2[3003]; vector < vector <int> > result; vector <int> cycle; void get_cycle(int i) { odw[i] = 1; cycle.push_back(i); if (odw[pos2[h[i]]] == 0) { get_cycle(pos2[h[i]]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> h[i]; h2[i] = h[i]; pos1[h[i]] = i; } sort(h2+1, h2+1+n); int out = 0; for (int i = 1; i <= n; i++) { pos2[h2[i]] = i; if (pos1[h2[i]] != pos2[h2[i]]) out++; } if (out == 0) { cout << "0"; return 0; } int count = 1; while (out > 0) { out = 0; for (int i = 1; i <= n; i++) { odw[i] = 0; } vector <int> begins, ends; for (int i = 1; i <= n; i++) { if (i != pos2[h[i]] && odw[i] == 0) { cycle.clear(); get_cycle(i); for (int j = 0; j < cycle.size()/2; j++) { int cur = cycle[j], cur2 = cycle[cycle.size()-j-1]; swap(h[cur], h[cur2]); begins.push_back(cur); ends.push_back(cur2); } cycle.clear(); } } vector <int> current; for (int i = 0; i < begins.size(); i++) { current.push_back(begins[i]); } for (int i = ends.size()-1; i >= 0; i--) { current.push_back(ends[i]); } result.push_back(current); for (int i = 1; i <= n; i++) { if (i != pos2[h[i]]) out++; } } cout << result.size() << "\n"; for (int i = 0; i < result.size(); i++) { cout << result[i].size() << "\n"; for (int j = 0; j < result[i].size(); j++) { cout << result[i][j] << " "; } 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 | #include <bits/stdc++.h> using namespace std; int h[3003], h2[3003], odw[3003]; int pos1[3003], pos2[3003]; vector < vector <int> > result; vector <int> cycle; void get_cycle(int i) { odw[i] = 1; cycle.push_back(i); if (odw[pos2[h[i]]] == 0) { get_cycle(pos2[h[i]]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> h[i]; h2[i] = h[i]; pos1[h[i]] = i; } sort(h2+1, h2+1+n); int out = 0; for (int i = 1; i <= n; i++) { pos2[h2[i]] = i; if (pos1[h2[i]] != pos2[h2[i]]) out++; } if (out == 0) { cout << "0"; return 0; } int count = 1; while (out > 0) { out = 0; for (int i = 1; i <= n; i++) { odw[i] = 0; } vector <int> begins, ends; for (int i = 1; i <= n; i++) { if (i != pos2[h[i]] && odw[i] == 0) { cycle.clear(); get_cycle(i); for (int j = 0; j < cycle.size()/2; j++) { int cur = cycle[j], cur2 = cycle[cycle.size()-j-1]; swap(h[cur], h[cur2]); begins.push_back(cur); ends.push_back(cur2); } cycle.clear(); } } vector <int> current; for (int i = 0; i < begins.size(); i++) { current.push_back(begins[i]); } for (int i = ends.size()-1; i >= 0; i--) { current.push_back(ends[i]); } result.push_back(current); for (int i = 1; i <= n; i++) { if (i != pos2[h[i]]) out++; } } cout << result.size() << "\n"; for (int i = 0; i < result.size(); i++) { cout << result[i].size() << "\n"; for (int j = 0; j < result[i].size(); j++) { cout << result[i][j] << " "; } cout << "\n"; } return 0; } |