#include <bits/stdc++.h> using namespace std; vector <int> convertToPerm(int n, vector <int> &h) { vector <pair<int,int>> vals(n); for (int i = 0; i < n; i++) { vals[i] = {h[i], i}; } sort(vals.begin(), vals.end()); vector <int> perm(n); for (int i = 0; i < n; i++) { perm[vals[i].second] = i; } return perm; } bool isIdentityPerm(int n, vector <int> &p) { for (int i = 0; i < n; i++) if (p[i] != i) { return false; } return true; } vector <int> getFirstGroup(int n, vector <int> &p) { vector <vector<int>> cycles; vector <bool> vis(n, false); for (int v = 0; v < n; v++) if (!vis[v]) { cycles.emplace_back(); int x = v; while (!vis[x]) { vis[x] = true; cycles.back().push_back(x); x = p[x]; } } deque <int> group; for (auto &cycle : cycles) { int cycleSize = cycle.size(); for (int i = 0; i < cycleSize - 1 - i; i++) { group.push_front(cycle[i]); group.push_back(cycle[cycleSize - 1 - i]); } } return vector <int> (group.begin(), group.end()); } vector <vector<int>> solveForPerm(int n, vector <int> &p) { if (isIdentityPerm(n, p)) { return {}; } vector <int> group = getFirstGroup(n, p); int groupSize = group.size(); for (int i = 0; i < groupSize - 1 - i; i++) { swap(p[group[i]], p[group[groupSize - 1 - i]]); } vector <vector<int>> groups = {group}; if (!isIdentityPerm(n, p)) { groups.push_back(getFirstGroup(n, p)); } return groups; } vector <vector<int>> solve(int n, vector <int> &h) { auto perm = convertToPerm(n, h); return solveForPerm(n, perm); } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector <int> h(n); for (int &hi : h) { cin >> hi; } auto ans = solve(n, h); cout << ans.size() << '\n'; for (auto &group : ans) { cout << group.size() << '\n'; for (int i : group) { cout << i + 1 << ' '; } 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 | #include <bits/stdc++.h> using namespace std; vector <int> convertToPerm(int n, vector <int> &h) { vector <pair<int,int>> vals(n); for (int i = 0; i < n; i++) { vals[i] = {h[i], i}; } sort(vals.begin(), vals.end()); vector <int> perm(n); for (int i = 0; i < n; i++) { perm[vals[i].second] = i; } return perm; } bool isIdentityPerm(int n, vector <int> &p) { for (int i = 0; i < n; i++) if (p[i] != i) { return false; } return true; } vector <int> getFirstGroup(int n, vector <int> &p) { vector <vector<int>> cycles; vector <bool> vis(n, false); for (int v = 0; v < n; v++) if (!vis[v]) { cycles.emplace_back(); int x = v; while (!vis[x]) { vis[x] = true; cycles.back().push_back(x); x = p[x]; } } deque <int> group; for (auto &cycle : cycles) { int cycleSize = cycle.size(); for (int i = 0; i < cycleSize - 1 - i; i++) { group.push_front(cycle[i]); group.push_back(cycle[cycleSize - 1 - i]); } } return vector <int> (group.begin(), group.end()); } vector <vector<int>> solveForPerm(int n, vector <int> &p) { if (isIdentityPerm(n, p)) { return {}; } vector <int> group = getFirstGroup(n, p); int groupSize = group.size(); for (int i = 0; i < groupSize - 1 - i; i++) { swap(p[group[i]], p[group[groupSize - 1 - i]]); } vector <vector<int>> groups = {group}; if (!isIdentityPerm(n, p)) { groups.push_back(getFirstGroup(n, p)); } return groups; } vector <vector<int>> solve(int n, vector <int> &h) { auto perm = convertToPerm(n, h); return solveForPerm(n, perm); } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector <int> h(n); for (int &hi : h) { cin >> hi; } auto ans = solve(n, h); cout << ans.size() << '\n'; for (auto &group : ans) { cout << group.size() << '\n'; for (int i : group) { cout << i + 1 << ' '; } cout << '\n'; } return 0; } |