#include <bits/stdc++.h> // #define MULTIPLE_TESTS // #define ENDLESS_TESTS #define TIME_LIMIT 1 // #define MEMORY_LIMIT 512 using namespace std; void test() { int length; cin >> length; vector<int> data(length); for (auto &v : data) { cin >> v; v -= 1; } { vector<int> indices(length); iota(indices.begin(), indices.end(), 0); sort(indices.begin(), indices.end(), [&](int lhs, int rhs) { return data[lhs] < data[rhs]; }); for (int i = 0; i < length; ++i) { data[indices[i]] = i; } } vector<vector<pair<int, int>>> answer(2); auto &first = answer[0]; auto &second = answer[1]; for (int start = 0; start < length; ++start) { if (data[start] == start) { continue; } vector<int> items = {start}; auto add = [&](auto &vec, int a, int b) { std::swap(data[a], data[b]); vec.emplace_back(a, b); }; auto fix = [&] { for (auto item : items) { // cout << item << ' '; cout << item << "[" << data[item] << "] "; // data[item] = item; } cout << endl; }; for(int item = data[start]; item != start; item = data[item]) { items.push_back(item); } if (items.size() == 2) { // fix(); add(first, items[0], items[1]); // fix(); // cout << "-----" << endl; continue; } const int n = items.size(); // fix(); { const int k = (n-1) / 2; const int m = (n % 2 == 0 ? n-2 : n-1); for (int i = 0; i < k; ++i) { add(first, items[i], items[m-i]); } } // fix(); { const int k0 = n % 2; const int k = k0 + n / 2; const int m = (n%2 == 0 ? n-1 : n); for (int i = k0; i < k; ++i) { add(second, items[i], items[m-i]); } } // fix(); // cout << "-----" << endl; } if (second.empty()) { answer.pop_back(); } if (first.empty()) { answer.pop_back(); } // cout << ":: "; for (auto v : data) cout << v << ' '; cout << endl; cout << answer.size() << '\n'; for (const auto &step : answer) { cout << 2 * step.size() << '\n'; for (auto it = step.begin(); it != step.end(); ++it) { cout << it->first + 1 << ' '; } for (auto it = step.rbegin(); it != step.rend(); ++it) { cout << it->second + 1 << ' '; } cout << '\n'; } } int main() { #ifndef CONTEST_WORKSPACE std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); #endif #ifdef ENDLESS_TESTS while(!(cin >> std::ws).eof()) test(); #else int T = 0; #ifdef MULTIPLE_TESTS cin >> T; #else T = 1; #endif while (T --> 0) test(); #endif 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 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | #include <bits/stdc++.h> // #define MULTIPLE_TESTS // #define ENDLESS_TESTS #define TIME_LIMIT 1 // #define MEMORY_LIMIT 512 using namespace std; void test() { int length; cin >> length; vector<int> data(length); for (auto &v : data) { cin >> v; v -= 1; } { vector<int> indices(length); iota(indices.begin(), indices.end(), 0); sort(indices.begin(), indices.end(), [&](int lhs, int rhs) { return data[lhs] < data[rhs]; }); for (int i = 0; i < length; ++i) { data[indices[i]] = i; } } vector<vector<pair<int, int>>> answer(2); auto &first = answer[0]; auto &second = answer[1]; for (int start = 0; start < length; ++start) { if (data[start] == start) { continue; } vector<int> items = {start}; auto add = [&](auto &vec, int a, int b) { std::swap(data[a], data[b]); vec.emplace_back(a, b); }; auto fix = [&] { for (auto item : items) { // cout << item << ' '; cout << item << "[" << data[item] << "] "; // data[item] = item; } cout << endl; }; for(int item = data[start]; item != start; item = data[item]) { items.push_back(item); } if (items.size() == 2) { // fix(); add(first, items[0], items[1]); // fix(); // cout << "-----" << endl; continue; } const int n = items.size(); // fix(); { const int k = (n-1) / 2; const int m = (n % 2 == 0 ? n-2 : n-1); for (int i = 0; i < k; ++i) { add(first, items[i], items[m-i]); } } // fix(); { const int k0 = n % 2; const int k = k0 + n / 2; const int m = (n%2 == 0 ? n-1 : n); for (int i = k0; i < k; ++i) { add(second, items[i], items[m-i]); } } // fix(); // cout << "-----" << endl; } if (second.empty()) { answer.pop_back(); } if (first.empty()) { answer.pop_back(); } // cout << ":: "; for (auto v : data) cout << v << ' '; cout << endl; cout << answer.size() << '\n'; for (const auto &step : answer) { cout << 2 * step.size() << '\n'; for (auto it = step.begin(); it != step.end(); ++it) { cout << it->first + 1 << ' '; } for (auto it = step.rbegin(); it != step.rend(); ++it) { cout << it->second + 1 << ' '; } cout << '\n'; } } int main() { #ifndef CONTEST_WORKSPACE std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); #endif #ifdef ENDLESS_TESTS while(!(cin >> std::ws).eof()) test(); #else int T = 0; #ifdef MULTIPLE_TESTS cin >> T; #else T = 1; #endif while (T --> 0) test(); #endif return 0; } |