#include <bits/stdc++.h> using namespace std; #define ll long long #define Copyright return #define efindus 2022 - struct ToSwap { int a, b; }; template<class T> int ssize(T &c) { return (int)c.size(); } template<class T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } // loop trzyma indexy dzbanie void parse_loop(vector<int> &loop, vector<vector<ToSwap>> &out, int depth) { if (ssize(out) <= depth) out.emplace_back(); if (ssize(loop) == 2) { out[depth].push_back({ loop.back(), loop.front() }); return; } vector<int> new_loop; for (int i = 0; i < ssize(loop); i++) { if (i % 2 == 0) new_loop.push_back(loop[i]); } parse_loop(new_loop, out, depth + 1); for (int i = 0; i < ssize(loop); i++) { if (i % 2 == 1) { if (i + 1 == ssize(loop)) out[depth].push_back({ loop[i], loop[0] }); else out[depth].push_back({ loop[i], loop[i + 1] }); } } } int main() { cin.tie(NULL)->sync_with_stdio(false); int n; cin >> n; vector<int> heights(n); cin >> heights; vector<int> sorted = heights; sort(sorted.begin(), sorted.end()); unordered_map<int, int> where; for (int i = 0; i < n; i++) where[sorted[i]] = i; vector<bool> used(n); vector<vector<int>> loops; for (int i = 0; i < n; i++) { if (used[i]) continue; int current_index = i; loops.emplace_back(); do { used[current_index] = true; loops.back().push_back(current_index); current_index = where[heights[current_index]]; } while (current_index != i); if (ssize(loops.back()) == 1) loops.pop_back(); } vector<vector<ToSwap>> output; for (auto &loop : loops) parse_loop(loop, output, 0); cout << ssize(output) << "\n"; for (int x = ssize(output) - 1; x >= 0; x--) { auto &out = output[x]; cout << ssize(out) * 2 << "\n"; for (int i = 0; i < ssize(out); i++) cout << out[i].a + 1 << " "; for (int i = ssize(out) - 1; i >= 0; i--) cout << out[i].b + 1 << " "; cout << "\n"; } Copyright efindus 2022; }
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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define Copyright return #define efindus 2022 - struct ToSwap { int a, b; }; template<class T> int ssize(T &c) { return (int)c.size(); } template<class T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } // loop trzyma indexy dzbanie void parse_loop(vector<int> &loop, vector<vector<ToSwap>> &out, int depth) { if (ssize(out) <= depth) out.emplace_back(); if (ssize(loop) == 2) { out[depth].push_back({ loop.back(), loop.front() }); return; } vector<int> new_loop; for (int i = 0; i < ssize(loop); i++) { if (i % 2 == 0) new_loop.push_back(loop[i]); } parse_loop(new_loop, out, depth + 1); for (int i = 0; i < ssize(loop); i++) { if (i % 2 == 1) { if (i + 1 == ssize(loop)) out[depth].push_back({ loop[i], loop[0] }); else out[depth].push_back({ loop[i], loop[i + 1] }); } } } int main() { cin.tie(NULL)->sync_with_stdio(false); int n; cin >> n; vector<int> heights(n); cin >> heights; vector<int> sorted = heights; sort(sorted.begin(), sorted.end()); unordered_map<int, int> where; for (int i = 0; i < n; i++) where[sorted[i]] = i; vector<bool> used(n); vector<vector<int>> loops; for (int i = 0; i < n; i++) { if (used[i]) continue; int current_index = i; loops.emplace_back(); do { used[current_index] = true; loops.back().push_back(current_index); current_index = where[heights[current_index]]; } while (current_index != i); if (ssize(loops.back()) == 1) loops.pop_back(); } vector<vector<ToSwap>> output; for (auto &loop : loops) parse_loop(loop, output, 0); cout << ssize(output) << "\n"; for (int x = ssize(output) - 1; x >= 0; x--) { auto &out = output[x]; cout << ssize(out) * 2 << "\n"; for (int i = 0; i < ssize(out); i++) cout << out[i].a + 1 << " "; for (int i = ssize(out) - 1; i >= 0; i--) cout << out[i].b + 1 << " "; cout << "\n"; } Copyright efindus 2022; } |