#include <bits/stdc++.h> using namespace std; int n; vector<int> tmpv; array<int, 3000> pos; vector<pair<vector<int>, vector<int>>> rounds; // void go(vector<int> &cycle, int i, int j) { // // cout << '.';second // if (i >= j) return; // int which = i + (j - i + 1) / 2; // // cout << "swapping " << cycle[i] << " with " << cycle[which] << endl; // rounds.back().first.push_back(cycle[i]); // rounds.back().second.push_back(cycle[which]); // go(cycle, i + 1, which - 1); // go(cycle, which + 1, j); // } bool onestep() { bool ok = true; for (int i = 1; i < n; i++) { if (tmpv[i] < tmpv[i - 1]) { ok = false; break; } } if (ok) return true; for (int &i: pos) i = 0; vector<int> perm(n); int which = 0; for (int i : tmpv) { perm[which++] = i; } vector<vector<int>> cycles; for (int i = 0; i < n; i++) { int target = perm[i]; if (target == -1) continue; cycles.emplace_back(); while (true) { cycles.back().push_back(target); int next_target = perm[target]; perm[target] = -1; if (target == i) break; target = next_target; } } // cout << cycles.size() << endl; // for (auto &cycle : cycles) { // for (int i : cycle) cout << i + 1 << ' '; // cout << endl; // } int rem_cycles = cycles.size(); for (int i = 0; i < rem_cycles; i++) { auto it = cycles[i].begin(); it++; if (it == cycles[i].end()) { swap(cycles[--rem_cycles], cycles[i--]); cycles.pop_back(); } } // if (!rem_cycles) return; rounds.emplace_back(); for (auto &cycle : cycles) { int i = 0, j = cycle.size() - 1; while (i < j) { rounds.back().first.push_back(cycle[i++]); rounds.back().second.push_back(cycle[j--]); } } int half = rounds.back().first.size(); for (int i = 0; i < half; i++) { swap(tmpv[rounds.back().first[i]], tmpv[rounds.back().second[i]]); } return false; } void solve() { cin >> n; tmpv = vector<int>(n); for (int &i : pos) i = 0; for (int i = 0; i < n; i++) { int v; cin >> v; // tmpv.push_back(v); pos[--v] = i + 1; } int which = 0; for (int i : pos) { if (i) { tmpv[i - 1] = which++; } } // for (int v : tmpv) cout << v << ' '; // cout << endl; while (!onestep()); int ans = rounds.size(); cout << ans << '\n'; for (int i = 0; i < ans; i++) { // for (int i = ans - 1; i >= 0; i--) { int halfsize = rounds[i].first.size(); cout << halfsize * 2 << '\n'; for (auto it = rounds[i].first.begin(); it != rounds[i].first.end(); it++) { cout << *it + 1 << ' '; } for (auto it = rounds[i].second.rbegin(); it != rounds[i].second.rend(); it++) { cout << *it + 1 << ' '; } cout << '\n'; } // for (int v : tmpv) cout << v << ' '; // cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); // #ifdef LOCAL // int t; cin >> t; while (t--) // #endif solve(); cout.flush(); }
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #include <bits/stdc++.h> using namespace std; int n; vector<int> tmpv; array<int, 3000> pos; vector<pair<vector<int>, vector<int>>> rounds; // void go(vector<int> &cycle, int i, int j) { // // cout << '.';second // if (i >= j) return; // int which = i + (j - i + 1) / 2; // // cout << "swapping " << cycle[i] << " with " << cycle[which] << endl; // rounds.back().first.push_back(cycle[i]); // rounds.back().second.push_back(cycle[which]); // go(cycle, i + 1, which - 1); // go(cycle, which + 1, j); // } bool onestep() { bool ok = true; for (int i = 1; i < n; i++) { if (tmpv[i] < tmpv[i - 1]) { ok = false; break; } } if (ok) return true; for (int &i: pos) i = 0; vector<int> perm(n); int which = 0; for (int i : tmpv) { perm[which++] = i; } vector<vector<int>> cycles; for (int i = 0; i < n; i++) { int target = perm[i]; if (target == -1) continue; cycles.emplace_back(); while (true) { cycles.back().push_back(target); int next_target = perm[target]; perm[target] = -1; if (target == i) break; target = next_target; } } // cout << cycles.size() << endl; // for (auto &cycle : cycles) { // for (int i : cycle) cout << i + 1 << ' '; // cout << endl; // } int rem_cycles = cycles.size(); for (int i = 0; i < rem_cycles; i++) { auto it = cycles[i].begin(); it++; if (it == cycles[i].end()) { swap(cycles[--rem_cycles], cycles[i--]); cycles.pop_back(); } } // if (!rem_cycles) return; rounds.emplace_back(); for (auto &cycle : cycles) { int i = 0, j = cycle.size() - 1; while (i < j) { rounds.back().first.push_back(cycle[i++]); rounds.back().second.push_back(cycle[j--]); } } int half = rounds.back().first.size(); for (int i = 0; i < half; i++) { swap(tmpv[rounds.back().first[i]], tmpv[rounds.back().second[i]]); } return false; } void solve() { cin >> n; tmpv = vector<int>(n); for (int &i : pos) i = 0; for (int i = 0; i < n; i++) { int v; cin >> v; // tmpv.push_back(v); pos[--v] = i + 1; } int which = 0; for (int i : pos) { if (i) { tmpv[i - 1] = which++; } } // for (int v : tmpv) cout << v << ' '; // cout << endl; while (!onestep()); int ans = rounds.size(); cout << ans << '\n'; for (int i = 0; i < ans; i++) { // for (int i = ans - 1; i >= 0; i--) { int halfsize = rounds[i].first.size(); cout << halfsize * 2 << '\n'; for (auto it = rounds[i].first.begin(); it != rounds[i].first.end(); it++) { cout << *it + 1 << ' '; } for (auto it = rounds[i].second.rbegin(); it != rounds[i].second.rend(); it++) { cout << *it + 1 << ' '; } cout << '\n'; } // for (int v : tmpv) cout << v << ' '; // cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); // #ifdef LOCAL // int t; cin >> t; while (t--) // #endif solve(); cout.flush(); } |