#include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<pair<int,int>> skal; for (int i = 0; i < n; i++) { int x; cin >> x; skal.emplace_back(x, i); } sort(skal.begin(), skal.end()); vector<int> perm(n); for (int i = 0; i < n; i++) perm[i] = skal[i].second; int biggest_cycle = 1; vector<bool> visited(n, false); vector<vector<int>> cycles; for (int i = 0; i < n; i++) { if (not visited[i]) { int l = 1; int ci = i; cycles.push_back({i}); while (perm[ci] != i) { l++; ci = perm[ci]; visited[ci] = true; cycles.back().push_back(ci); } biggest_cycle = max(l, biggest_cycle); visited[i] = true; } } /* for (auto cycle : cycles) { for (int x : cycle) cerr << x << " "; cerr << "\n"; } */ int l = min(biggest_cycle - 1, 2); cout << l << "\n"; for (int rnd = 1; rnd <= l; rnd++) { deque<int> query; for (vector<int> cycle : cycles) { int len = cycle.size(); if (rnd == 2) { for (int i = 0; i * 2 < len; i++) { if (i != (len - i) % len) { query.push_front(cycle[i]); query.push_back(cycle[(len - i) % len]); } } } if (rnd == 1) { for (int i = 1; i * 2 <= len; i++) { int x1 = i, x2 = (len + 1 - i) % len; if (x1 != x2) { query.push_front(cycle[i]); query.push_back(cycle[(1 - i + len) % len]); } } } } cout << query.size() << "\n"; for (int x : query) cout << x + 1 << " "; cout << "\n"; } }
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 | #include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<pair<int,int>> skal; for (int i = 0; i < n; i++) { int x; cin >> x; skal.emplace_back(x, i); } sort(skal.begin(), skal.end()); vector<int> perm(n); for (int i = 0; i < n; i++) perm[i] = skal[i].second; int biggest_cycle = 1; vector<bool> visited(n, false); vector<vector<int>> cycles; for (int i = 0; i < n; i++) { if (not visited[i]) { int l = 1; int ci = i; cycles.push_back({i}); while (perm[ci] != i) { l++; ci = perm[ci]; visited[ci] = true; cycles.back().push_back(ci); } biggest_cycle = max(l, biggest_cycle); visited[i] = true; } } /* for (auto cycle : cycles) { for (int x : cycle) cerr << x << " "; cerr << "\n"; } */ int l = min(biggest_cycle - 1, 2); cout << l << "\n"; for (int rnd = 1; rnd <= l; rnd++) { deque<int> query; for (vector<int> cycle : cycles) { int len = cycle.size(); if (rnd == 2) { for (int i = 0; i * 2 < len; i++) { if (i != (len - i) % len) { query.push_front(cycle[i]); query.push_back(cycle[(len - i) % len]); } } } if (rnd == 1) { for (int i = 1; i * 2 <= len; i++) { int x1 = i, x2 = (len + 1 - i) % len; if (x1 != x2) { query.push_front(cycle[i]); query.push_back(cycle[(1 - i + len) % len]); } } } } cout << query.size() << "\n"; for (int x : query) cout << x + 1 << " "; cout << "\n"; } } |