#include <bits/stdc++.h> #define st first #define nd second #define pb push_back using namespace std; typedef long long ll; typedef pair<ll, ll> pll; bool isSorted(vector<ll> &v) { for (int i = 0; i < v.size() - 1; ++i) { if (v[i] > v[i+1]) return false; } return true; } int main() { ios_base::sync_with_stdio(0); ll n; cin >> n; vector<vector<ll> > results; vector<ll> res(n); vector<pll> p(n); vector<ll> g(n); vector<bool> vis(n, false); for (int i = 0; i < n; ++i) { cin >> res[i]; p[i].st = res[i]; p[i].nd = i; } sort(p.begin(), p.end()); for (int i = 0; i < n; ++i) { g[p[i].nd] = i; } vector<pll> pairs; do { pairs.clear(); for (int i = 0; i < n; ++i) vis[i] = false; vector<ll> chain; for (int i = 0; i < n; ++i) if (!vis[i]) { ll pomi = i; chain.clear(); while (!vis[pomi]) { chain.pb(pomi); vis[pomi] = true; pomi = g[pomi]; } if (chain.size() == 1) continue; if (chain.size() == 2) { pairs.pb({chain[0], chain[1]}); } else { for (int i = 0; 2*i + 2 < chain.size(); ++i) { pairs.pb({chain[i + 1], chain[chain.size() - 1 - i]}); } } } for (int i = 0; i < pairs.size(); ++i) { swap(g[pairs[i].st], g[pairs[i].nd]); } vector<ll> resultPart; for (int i = 0; i < pairs.size(); ++i) { resultPart.pb(pairs[i].st + 1); } for (int i = pairs.size() - 1; i >= 0; --i) { resultPart.pb(pairs[i].nd + 1); } if (!resultPart.empty()) results.pb(resultPart); for (int i = 0; i < pairs.size(); ++i) { swap(res[pairs[i].st], res[pairs[i].nd]); } } while (!isSorted(res)); cout << results.size() << endl; for (int i = 0; i < results.size(); ++i) { cout << results[i].size() << endl; for (int j = 0; j < results[i].size(); ++j) { cout << results[i][j] << ' '; } 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #include <bits/stdc++.h> #define st first #define nd second #define pb push_back using namespace std; typedef long long ll; typedef pair<ll, ll> pll; bool isSorted(vector<ll> &v) { for (int i = 0; i < v.size() - 1; ++i) { if (v[i] > v[i+1]) return false; } return true; } int main() { ios_base::sync_with_stdio(0); ll n; cin >> n; vector<vector<ll> > results; vector<ll> res(n); vector<pll> p(n); vector<ll> g(n); vector<bool> vis(n, false); for (int i = 0; i < n; ++i) { cin >> res[i]; p[i].st = res[i]; p[i].nd = i; } sort(p.begin(), p.end()); for (int i = 0; i < n; ++i) { g[p[i].nd] = i; } vector<pll> pairs; do { pairs.clear(); for (int i = 0; i < n; ++i) vis[i] = false; vector<ll> chain; for (int i = 0; i < n; ++i) if (!vis[i]) { ll pomi = i; chain.clear(); while (!vis[pomi]) { chain.pb(pomi); vis[pomi] = true; pomi = g[pomi]; } if (chain.size() == 1) continue; if (chain.size() == 2) { pairs.pb({chain[0], chain[1]}); } else { for (int i = 0; 2*i + 2 < chain.size(); ++i) { pairs.pb({chain[i + 1], chain[chain.size() - 1 - i]}); } } } for (int i = 0; i < pairs.size(); ++i) { swap(g[pairs[i].st], g[pairs[i].nd]); } vector<ll> resultPart; for (int i = 0; i < pairs.size(); ++i) { resultPart.pb(pairs[i].st + 1); } for (int i = pairs.size() - 1; i >= 0; --i) { resultPart.pb(pairs[i].nd + 1); } if (!resultPart.empty()) results.pb(resultPart); for (int i = 0; i < pairs.size(); ++i) { swap(res[pairs[i].st], res[pairs[i].nd]); } } while (!isSorted(res)); cout << results.size() << endl; for (int i = 0; i < results.size(); ++i) { cout << results[i].size() << endl; for (int j = 0; j < results[i].size(); ++j) { cout << results[i][j] << ' '; } cout << endl; } } |