#include <iostream> #include <vector> #include <set> #include <algorithm> #include <deque> int main() { std::ios_base::sync_with_stdio(0); int n; std::cin >> n; std::vector<int> h(n); std::vector<int> p_rev(n); for (int i = 0; i < n; ++i) { std::cin >> h[i]; p_rev[i] = i; } std::sort(p_rev.begin(), p_rev.end(), [&h](int a, int b) { return h[a] < h[b]; }); std::vector<int> p(n); for (int i = 0; i < p.size(); ++i) { p[p_rev[i]] = i; } std::vector<std::deque<int>> ans; while (true) { std::vector<std::vector<int>> cycles; std::vector<bool> mask(n); for (int i = 0; i < n; ++i) { if (!mask[i] && p[i] != i) { mask[i] = true; cycles.push_back({i}); int cur = i; while (p[cur] != i) { cur = p[cur]; mask[cur] = true; cycles.back().push_back(cur); } } } if (cycles.size() == 0) break; std::deque<int> r; for (auto const& cycle : cycles) { for (int i = 0; i < cycle.size() / 2; ++i) { r.push_front(cycle[i]); r.push_back(cycle[cycle.size() - 1 - i]); } } ans.push_back(r); // for (int x : p) std::cout<<x<<' '; // std::cout<<"\n"; // for (int x : r) std::cout<<x<<' '; // std::cout<<"\n"; auto p2 = p; for (int i = 0; i < r.size(); ++i) { p[r[r.size() - 1 - i]] = p2[r[i]]; } // for (int x : p) std::cout<<x<<' '; // std::cout<<"\n"; // std::cout<<"\n"; } std::cout << ans.size() << '\n'; for (auto const& r : ans) { std::cout << r.size() << '\n'; for (auto i : r) { std::cout << i + 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 | #include <iostream> #include <vector> #include <set> #include <algorithm> #include <deque> int main() { std::ios_base::sync_with_stdio(0); int n; std::cin >> n; std::vector<int> h(n); std::vector<int> p_rev(n); for (int i = 0; i < n; ++i) { std::cin >> h[i]; p_rev[i] = i; } std::sort(p_rev.begin(), p_rev.end(), [&h](int a, int b) { return h[a] < h[b]; }); std::vector<int> p(n); for (int i = 0; i < p.size(); ++i) { p[p_rev[i]] = i; } std::vector<std::deque<int>> ans; while (true) { std::vector<std::vector<int>> cycles; std::vector<bool> mask(n); for (int i = 0; i < n; ++i) { if (!mask[i] && p[i] != i) { mask[i] = true; cycles.push_back({i}); int cur = i; while (p[cur] != i) { cur = p[cur]; mask[cur] = true; cycles.back().push_back(cur); } } } if (cycles.size() == 0) break; std::deque<int> r; for (auto const& cycle : cycles) { for (int i = 0; i < cycle.size() / 2; ++i) { r.push_front(cycle[i]); r.push_back(cycle[cycle.size() - 1 - i]); } } ans.push_back(r); // for (int x : p) std::cout<<x<<' '; // std::cout<<"\n"; // for (int x : r) std::cout<<x<<' '; // std::cout<<"\n"; auto p2 = p; for (int i = 0; i < r.size(); ++i) { p[r[r.size() - 1 - i]] = p2[r[i]]; } // for (int x : p) std::cout<<x<<' '; // std::cout<<"\n"; // std::cout<<"\n"; } std::cout << ans.size() << '\n'; for (auto const& r : ans) { std::cout << r.size() << '\n'; for (auto i : r) { std::cout << i + 1 << ' '; } std::cout << '\n'; } return 0; } |