#include <algorithm> #include <cstdint> #include <deque> #include <ios> #include <iostream> #include <iterator> #include <limits> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> template <typename T> void print_value(const T &v); template <typename T> void debug(const T &v, const std::string &s) { std::cerr << s << ": '"; print_value(v); std::cerr << "'\n"; } template <> void print_value(const std::vector<int64_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <> void print_value(const std::deque<int> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <> void print_value(const std::vector<int> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <> void print_value(const std::unordered_set<int32_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <typename T> void print_value(const T &v) { std::cerr << v; } void prelude() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); } int count_bits(unsigned int x) { int count = 0; while (x) { count += x & 1; x >>= 1; } return count; } int main() { prelude(); int n = 0; std::cin >> n; std::vector<std::pair<int, int>> h; for (int i = 0; i < n; ++i) { int tmp; std::cin >> tmp; h.push_back(std::make_pair(tmp, i)); } std::sort(h.begin(), h.end()); std::vector<int> h_order(n); for (int i = 0; i < n; ++i) { h_order[h[i].second] = i; } // 0-based. std::vector<std::deque<int>> swap_rounds; while (!std::is_sorted(h_order.begin(), h_order.end())) { std::vector<bool> visited(n, false); std::deque<int> swaps_prepared; for (int i = 0; i < n; ++i) { if (!visited[i]) { if (h_order[i] != i) { std::vector<int> cycle; int j = i; do { visited[j] = true; cycle.push_back(j); j = h_order[j]; } while (j != i); for(int k = 0; k < cycle.size()/2; ++k) { swaps_prepared.push_back(cycle[k]); swaps_prepared.push_front(cycle[cycle.size()-k-1]); } } } } for (int i = 0; i < swaps_prepared.size() / 2; ++i) { int a = swaps_prepared[i]; int b = swaps_prepared[swaps_prepared.size() - i - 1]; std::swap(h_order[a], h_order[b]); } swap_rounds.emplace_back(std::move(swaps_prepared)); } std::cout << swap_rounds.size() << "\n"; for (const auto &round : swap_rounds) { std::cout << round.size() << "\n"; for (int i = 0; i < round.size() - 1; ++i) { std::cout << round[i] + 1 << " "; } std::cout << round.back() + 1 << "\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 115 | #include <algorithm> #include <cstdint> #include <deque> #include <ios> #include <iostream> #include <iterator> #include <limits> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> template <typename T> void print_value(const T &v); template <typename T> void debug(const T &v, const std::string &s) { std::cerr << s << ": '"; print_value(v); std::cerr << "'\n"; } template <> void print_value(const std::vector<int64_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <> void print_value(const std::deque<int> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <> void print_value(const std::vector<int> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <> void print_value(const std::unordered_set<int32_t> &v) { for (const auto x : v) { std::cerr << x << " "; } } template <typename T> void print_value(const T &v) { std::cerr << v; } void prelude() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); } int count_bits(unsigned int x) { int count = 0; while (x) { count += x & 1; x >>= 1; } return count; } int main() { prelude(); int n = 0; std::cin >> n; std::vector<std::pair<int, int>> h; for (int i = 0; i < n; ++i) { int tmp; std::cin >> tmp; h.push_back(std::make_pair(tmp, i)); } std::sort(h.begin(), h.end()); std::vector<int> h_order(n); for (int i = 0; i < n; ++i) { h_order[h[i].second] = i; } // 0-based. std::vector<std::deque<int>> swap_rounds; while (!std::is_sorted(h_order.begin(), h_order.end())) { std::vector<bool> visited(n, false); std::deque<int> swaps_prepared; for (int i = 0; i < n; ++i) { if (!visited[i]) { if (h_order[i] != i) { std::vector<int> cycle; int j = i; do { visited[j] = true; cycle.push_back(j); j = h_order[j]; } while (j != i); for(int k = 0; k < cycle.size()/2; ++k) { swaps_prepared.push_back(cycle[k]); swaps_prepared.push_front(cycle[cycle.size()-k-1]); } } } } for (int i = 0; i < swaps_prepared.size() / 2; ++i) { int a = swaps_prepared[i]; int b = swaps_prepared[swaps_prepared.size() - i - 1]; std::swap(h_order[a], h_order[b]); } swap_rounds.emplace_back(std::move(swaps_prepared)); } std::cout << swap_rounds.size() << "\n"; for (const auto &round : swap_rounds) { std::cout << round.size() << "\n"; for (int i = 0; i < round.size() - 1; ++i) { std::cout << round[i] + 1 << " "; } std::cout << round.back() + 1 << "\n"; } return 0; } |