#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii_t; typedef vector<pii_t> round_t; void remap_height(vector<int>& height) { vector<pair<int,int>> v(height.size()); for (int i = 0; i < (int)height.size(); i++) v[i] = {height[i], i}; sort(v.begin(), v.end()); for (int i = 0; i < (int)v.size(); i++) height[v[i].second] = i; } vector<int> build_round(const round_t& swaps) { vector<int> result(2 * swaps.size()); for (int i = 0; i < (int)swaps.size(); i++) { result[i] = swaps[i].first; result[result.size()-1-i] = swaps[i].second; } return result; } pair<round_t, round_t> resolve_cycle(const vector<int>& cycle) { round_t r1, r2; if (cycle.size() == 1) return {r1, r2}; else if (cycle.size() == 2) { r1.push_back({cycle[0], cycle[1]}); return {r1, r2}; } // cycle.size() >= 3 int mid = cycle.size() / 2; r1.push_back({cycle[mid-1], cycle[mid]}); int left = mid-2, right = mid+1; while (0 <= left && right < (int)cycle.size()) { r1.push_back({cycle[left], cycle[right]}); r2.push_back({cycle[left+1], cycle[right]}); left--, right++; } if (cycle.size() % 2 == 1) r2.push_back({cycle.front(), cycle.back()}); return {r1, r2}; } vector<vector<int>> get_cycles(const vector<int>& p) { vector<vector<int>> cycles; vector<bool> used(p.size(), false); for (int i = 0; i < (int)p.size(); i++) if (!used[i]) { used[i] = true; vector<int> c = {i}; while (p[c.back()] != c.front()) { used[p[c.back()]] = true; c.push_back(p[c.back()]); } cycles.push_back(c); } return cycles; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> p(n); for (int i = 0; i < (int)p.size(); i++) { cin >> p[i]; p[i]--; } remap_height(p); vector<vector<int>> cycles = get_cycles(p); round_t r1, r2; for (const auto& c : cycles) { auto [r1_part, r2_part] = resolve_cycle(c); for (const auto& e : r1_part) r1.push_back(e); for (const auto& e : r2_part) r2.push_back(e); } if (r1.empty()) cout << "0\n"; else if (r2.empty()) { cout << "1\n"; vector<int> round1 = build_round(r1); cout << round1.size() << "\n"; for (const auto& e : round1) cout << e+1 << " "; cout << "\n"; } else { cout << "2\n"; vector<int> round1 = build_round(r1); cout << round1.size() << "\n"; for (const auto& e : round1) cout << e+1 << " "; cout << "\n"; vector<int> round2 = build_round(r2); cout << round2.size() << "\n"; for (const auto& e : round2) cout << e+1 << " "; cout << "\n"; } }
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 134 135 136 137 138 139 | #include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii_t; typedef vector<pii_t> round_t; void remap_height(vector<int>& height) { vector<pair<int,int>> v(height.size()); for (int i = 0; i < (int)height.size(); i++) v[i] = {height[i], i}; sort(v.begin(), v.end()); for (int i = 0; i < (int)v.size(); i++) height[v[i].second] = i; } vector<int> build_round(const round_t& swaps) { vector<int> result(2 * swaps.size()); for (int i = 0; i < (int)swaps.size(); i++) { result[i] = swaps[i].first; result[result.size()-1-i] = swaps[i].second; } return result; } pair<round_t, round_t> resolve_cycle(const vector<int>& cycle) { round_t r1, r2; if (cycle.size() == 1) return {r1, r2}; else if (cycle.size() == 2) { r1.push_back({cycle[0], cycle[1]}); return {r1, r2}; } // cycle.size() >= 3 int mid = cycle.size() / 2; r1.push_back({cycle[mid-1], cycle[mid]}); int left = mid-2, right = mid+1; while (0 <= left && right < (int)cycle.size()) { r1.push_back({cycle[left], cycle[right]}); r2.push_back({cycle[left+1], cycle[right]}); left--, right++; } if (cycle.size() % 2 == 1) r2.push_back({cycle.front(), cycle.back()}); return {r1, r2}; } vector<vector<int>> get_cycles(const vector<int>& p) { vector<vector<int>> cycles; vector<bool> used(p.size(), false); for (int i = 0; i < (int)p.size(); i++) if (!used[i]) { used[i] = true; vector<int> c = {i}; while (p[c.back()] != c.front()) { used[p[c.back()]] = true; c.push_back(p[c.back()]); } cycles.push_back(c); } return cycles; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> p(n); for (int i = 0; i < (int)p.size(); i++) { cin >> p[i]; p[i]--; } remap_height(p); vector<vector<int>> cycles = get_cycles(p); round_t r1, r2; for (const auto& c : cycles) { auto [r1_part, r2_part] = resolve_cycle(c); for (const auto& e : r1_part) r1.push_back(e); for (const auto& e : r2_part) r2.push_back(e); } if (r1.empty()) cout << "0\n"; else if (r2.empty()) { cout << "1\n"; vector<int> round1 = build_round(r1); cout << round1.size() << "\n"; for (const auto& e : round1) cout << e+1 << " "; cout << "\n"; } else { cout << "2\n"; vector<int> round1 = build_round(r1); cout << round1.size() << "\n"; for (const auto& e : round1) cout << e+1 << " "; cout << "\n"; vector<int> round2 = build_round(r2); cout << round2.size() << "\n"; for (const auto& e : round2) cout << e+1 << " "; cout << "\n"; } } |