#include <iostream> #include <vector> #include <algorithm> #include <utility> int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); int n; std::cin >> n; std::vector<int> input(n); std::vector<std::pair<int,int>> sorted(n); for (int i = 0; i < n; ++i) { std::cin >> input[i]; sorted[i] = {input[i], i}; } std::sort(sorted.begin(), sorted.end()); std::vector<int> sortperm(n); for (int i = 0; i < n; ++i) { sortperm[sorted[i].second] = i; } std::vector<std::vector<int>> cycles; std::vector<bool> marked(n); int max_cycle_len = 0; for (int i = 0; i < n; ++i) { if (marked[i] || sortperm[i] == i) continue; std::vector<int> cycle; cycle.push_back(i); marked[i] = true; int v = sortperm[i]; while (v != i) { cycle.push_back(v); marked[v] = true; v = sortperm[v]; } max_cycle_len = std::max(max_cycle_len, (int)cycle.size()); cycles.push_back(std::move(cycle)); } /* std::cout << '\n'; std::cout << cycles.size() << " cycles.\n"; for (auto &cycle : cycles) { for (int x : cycle) std::cout << x << ' '; std::cout << '\n'; } std::cout << "======\n"; */ std::vector<std::vector<std::pair<int,int>>> moves; { std::vector<std::pair<int,int>> swaps; for (auto &cyc : cycles) { int k = cyc.size(); for (int i = 0; i < k/2; ++i) { // std::cout << "swap(input[" << cyc[i] << "], input[" << cyc[k-1 - i] << "])\n"; swaps.emplace_back(cyc[i], cyc[k-1 - i]); std::swap(input[cyc[i]], input[cyc[k-1 - i]]); } } if (!swaps.empty()) moves.push_back(std::move(swaps)); } // std::cout << "======\n"; for (int i = 0; i < n; ++i) { sorted[i] = {input[i], i}; } std::sort(sorted.begin(), sorted.end()); for (int i = 0; i < n; ++i) { sortperm[sorted[i].second] = i; } // std::cout << "sorting permutation:"; // for (int x : sortperm) std::cout << " " << x; // std::cout << '\n'; // std::cout << "======\n"; marked.assign(n, false); { std::vector<std::pair<int,int>> swaps; for (int i = 0; i < n; ++i) { if (marked[i] || sortperm[i] == i) continue; // std::cout << "swap(input[" << sortperm[i] << "], input[" << i << "])\n"; swaps.emplace_back(sortperm[i], i); marked[i] = marked[sortperm[i]] = true; } if (!swaps.empty()) moves.push_back(std::move(swaps)); } std::cout << moves.size() << '\n'; for (auto &swaps : moves) { int k = swaps.size(); std::vector<int> move(2*k); for (int i = 0; i < k; ++i) { move[i] = swaps[i].first; move[2*k-1 - i] = swaps[i].second; } std::cout << 2*k << '\n'; for (int x : move) std::cout << x+1 << ' '; std::cout << '\n'; } 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 | #include <iostream> #include <vector> #include <algorithm> #include <utility> int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); int n; std::cin >> n; std::vector<int> input(n); std::vector<std::pair<int,int>> sorted(n); for (int i = 0; i < n; ++i) { std::cin >> input[i]; sorted[i] = {input[i], i}; } std::sort(sorted.begin(), sorted.end()); std::vector<int> sortperm(n); for (int i = 0; i < n; ++i) { sortperm[sorted[i].second] = i; } std::vector<std::vector<int>> cycles; std::vector<bool> marked(n); int max_cycle_len = 0; for (int i = 0; i < n; ++i) { if (marked[i] || sortperm[i] == i) continue; std::vector<int> cycle; cycle.push_back(i); marked[i] = true; int v = sortperm[i]; while (v != i) { cycle.push_back(v); marked[v] = true; v = sortperm[v]; } max_cycle_len = std::max(max_cycle_len, (int)cycle.size()); cycles.push_back(std::move(cycle)); } /* std::cout << '\n'; std::cout << cycles.size() << " cycles.\n"; for (auto &cycle : cycles) { for (int x : cycle) std::cout << x << ' '; std::cout << '\n'; } std::cout << "======\n"; */ std::vector<std::vector<std::pair<int,int>>> moves; { std::vector<std::pair<int,int>> swaps; for (auto &cyc : cycles) { int k = cyc.size(); for (int i = 0; i < k/2; ++i) { // std::cout << "swap(input[" << cyc[i] << "], input[" << cyc[k-1 - i] << "])\n"; swaps.emplace_back(cyc[i], cyc[k-1 - i]); std::swap(input[cyc[i]], input[cyc[k-1 - i]]); } } if (!swaps.empty()) moves.push_back(std::move(swaps)); } // std::cout << "======\n"; for (int i = 0; i < n; ++i) { sorted[i] = {input[i], i}; } std::sort(sorted.begin(), sorted.end()); for (int i = 0; i < n; ++i) { sortperm[sorted[i].second] = i; } // std::cout << "sorting permutation:"; // for (int x : sortperm) std::cout << " " << x; // std::cout << '\n'; // std::cout << "======\n"; marked.assign(n, false); { std::vector<std::pair<int,int>> swaps; for (int i = 0; i < n; ++i) { if (marked[i] || sortperm[i] == i) continue; // std::cout << "swap(input[" << sortperm[i] << "], input[" << i << "])\n"; swaps.emplace_back(sortperm[i], i); marked[i] = marked[sortperm[i]] = true; } if (!swaps.empty()) moves.push_back(std::move(swaps)); } std::cout << moves.size() << '\n'; for (auto &swaps : moves) { int k = swaps.size(); std::vector<int> move(2*k); for (int i = 0; i < k; ++i) { move[i] = swaps[i].first; move[2*k-1 - i] = swaps[i].second; } std::cout << 2*k << '\n'; for (int x : move) std::cout << x+1 << ' '; std::cout << '\n'; } return 0; } |