#include <iostream> #include <vector> #include <algorithm> using namespace std; struct ele { int a, nr; }; bool por1(ele a, ele b) { return a.a < b.a; } bool por2(ele a, ele b) { return a.nr < b.nr; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<ele> a(n); for (int i = 0; i < n; ++i) { cin >> a[i].a; a[i].nr = i; } sort(a.begin(), a.end(), por1); for (int i = 0; i < n; ++i) a[i].a = i; sort(a.begin(), a.end(), por2); bool ok = true; for (int i = 0; i < n; ++i) if (a[i].a != i) ok = false; if (ok) { cout << "0"; return 0; } vector< vector<int> > odp; for (int i = 0; i < n; ++i) { vector<int> pocz, kon; vector<int> gdzie(n); for (int j = 0; j < n; ++j) gdzie[a[j].a] = j; for (int j = 0; j < n; ++j) { vector<int> P, K; vector<bool> uzywam(n, false); for (int k = j; k < n; ++k) { if (a[k].a == k || uzywam[k] || uzywam[gdzie[k]]) continue; P.push_back(k); K.push_back(gdzie[k]); uzywam[k] = uzywam[gdzie[k]] = true; } if (P.size() > pocz.size()) { pocz = P; kon = K; } } vector<int> pusty; odp.push_back(pusty); for (int j = 0; j < pocz.size(); ++j) odp.back().push_back(pocz[j]); for (int j = kon.size() - 1; j >= 0; --j) odp.back().push_back(kon[j]); vector<int> c; for (int j = 0; j < odp.back().size(); ++j) c.push_back(a[odp.back()[j]].a); for (int j = c.size() - 1, k = 0; j >= 0; --j, ++k) a[odp.back()[k]].a = c[j]; } while (odp.back().size() == 0) odp.pop_back(); cout << odp.size() << endl; for (int i = 0; i < odp.size(); ++i) { cout << odp[i].size() << endl; for (int j = 0; j < odp[i].size(); ++j) cout << odp[i][j] + 1 << " "; cout << endl; } 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; struct ele { int a, nr; }; bool por1(ele a, ele b) { return a.a < b.a; } bool por2(ele a, ele b) { return a.nr < b.nr; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<ele> a(n); for (int i = 0; i < n; ++i) { cin >> a[i].a; a[i].nr = i; } sort(a.begin(), a.end(), por1); for (int i = 0; i < n; ++i) a[i].a = i; sort(a.begin(), a.end(), por2); bool ok = true; for (int i = 0; i < n; ++i) if (a[i].a != i) ok = false; if (ok) { cout << "0"; return 0; } vector< vector<int> > odp; for (int i = 0; i < n; ++i) { vector<int> pocz, kon; vector<int> gdzie(n); for (int j = 0; j < n; ++j) gdzie[a[j].a] = j; for (int j = 0; j < n; ++j) { vector<int> P, K; vector<bool> uzywam(n, false); for (int k = j; k < n; ++k) { if (a[k].a == k || uzywam[k] || uzywam[gdzie[k]]) continue; P.push_back(k); K.push_back(gdzie[k]); uzywam[k] = uzywam[gdzie[k]] = true; } if (P.size() > pocz.size()) { pocz = P; kon = K; } } vector<int> pusty; odp.push_back(pusty); for (int j = 0; j < pocz.size(); ++j) odp.back().push_back(pocz[j]); for (int j = kon.size() - 1; j >= 0; --j) odp.back().push_back(kon[j]); vector<int> c; for (int j = 0; j < odp.back().size(); ++j) c.push_back(a[odp.back()[j]].a); for (int j = c.size() - 1, k = 0; j >= 0; --j, ++k) a[odp.back()[k]].a = c[j]; } while (odp.back().size() == 0) odp.pop_back(); cout << odp.size() << endl; for (int i = 0; i < odp.size(); ++i) { cout << odp[i].size() << endl; for (int j = 0; j < odp[i].size(); ++j) cout << odp[i][j] + 1 << " "; cout << endl; } return 0; } |