#include <bits/stdc++.h> using namespace std; int n; void renumber(vector<int> &v) { vector<int> sorted(v); sort(sorted.begin(), sorted.end()); for (int &i : v) i = lower_bound(sorted.begin(), sorted.end(), i) - sorted.begin(); } vector<int> inverse(vector<int> &v) { vector<int> inv(n); for (int i = 0; i < n; ++i) inv[v[i]] = i; return inv; } vector<vector<int>> cycle_decomp(const vector<int> &v) { vector<vector<int>> cycles; vector<bool> visited(n, false); for (int i = 0; i < n; ++i) { if (visited[i]) continue; cycles.emplace_back(); for (int j = i; !visited[j]; j = v[j]) { visited[j] = true; cycles.back().push_back(j); } } return cycles; } void handle_2(vector<vector<int>> &cycles, vector<int> &v) { cout << "2\n"; vector<pair<int, int>> tpos; for (vector<int> &cyc : cycles) { int l = cyc.size(); if (l == 2) continue; for (int i = 1; i < l-i; ++i) tpos.push_back({cyc[l-i], cyc[i]}); } cout << tpos.size() * 2 << '\n'; for (int i = 0; i < tpos.size(); ++i) cout << tpos[i].first+1 << ' '; for (int i = 0; i < tpos.size(); ++i) cout << tpos[tpos.size()-1-i].second+1 << ' '; cout << '\n'; for (auto &[x, y] : tpos) swap(v[x], v[y]); cycles = cycle_decomp(v); tpos.clear(); for (auto &i : cycles) if (i.size() > 1) tpos.push_back({i[0], i[1]}); cout << tpos.size() * 2 << '\n'; for (int i = 0; i < tpos.size(); ++i) cout << tpos[i].first+1 << ' '; for (int i = 0; i < tpos.size(); ++i) cout << tpos[tpos.size()-1-i].second+1 << ' '; cout << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; vector<int> v(n); for (int &i : v) cin >> i; renumber(v); // v = inverse(v); vector<vector<int>> cycles = cycle_decomp(v); for (auto &i : cycles) { if (i.size() > 2) { handle_2(cycles, v); return 0; } } vector<pair<int, int>> tpos; for (auto &i : cycles) if (i.size() > 1) tpos.push_back({i[0], i[1]}); if (tpos.size() == 0) { cout << "0\n"; return 0; } cout << "1\n"; cout << tpos.size() * 2 << '\n'; for (int i = 0; i < tpos.size(); ++i) cout << tpos[i].first+1 << ' '; for (int i = 0; i < tpos.size(); ++i) cout << tpos[tpos.size()-1-i].second+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 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 | #include <bits/stdc++.h> using namespace std; int n; void renumber(vector<int> &v) { vector<int> sorted(v); sort(sorted.begin(), sorted.end()); for (int &i : v) i = lower_bound(sorted.begin(), sorted.end(), i) - sorted.begin(); } vector<int> inverse(vector<int> &v) { vector<int> inv(n); for (int i = 0; i < n; ++i) inv[v[i]] = i; return inv; } vector<vector<int>> cycle_decomp(const vector<int> &v) { vector<vector<int>> cycles; vector<bool> visited(n, false); for (int i = 0; i < n; ++i) { if (visited[i]) continue; cycles.emplace_back(); for (int j = i; !visited[j]; j = v[j]) { visited[j] = true; cycles.back().push_back(j); } } return cycles; } void handle_2(vector<vector<int>> &cycles, vector<int> &v) { cout << "2\n"; vector<pair<int, int>> tpos; for (vector<int> &cyc : cycles) { int l = cyc.size(); if (l == 2) continue; for (int i = 1; i < l-i; ++i) tpos.push_back({cyc[l-i], cyc[i]}); } cout << tpos.size() * 2 << '\n'; for (int i = 0; i < tpos.size(); ++i) cout << tpos[i].first+1 << ' '; for (int i = 0; i < tpos.size(); ++i) cout << tpos[tpos.size()-1-i].second+1 << ' '; cout << '\n'; for (auto &[x, y] : tpos) swap(v[x], v[y]); cycles = cycle_decomp(v); tpos.clear(); for (auto &i : cycles) if (i.size() > 1) tpos.push_back({i[0], i[1]}); cout << tpos.size() * 2 << '\n'; for (int i = 0; i < tpos.size(); ++i) cout << tpos[i].first+1 << ' '; for (int i = 0; i < tpos.size(); ++i) cout << tpos[tpos.size()-1-i].second+1 << ' '; cout << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; vector<int> v(n); for (int &i : v) cin >> i; renumber(v); // v = inverse(v); vector<vector<int>> cycles = cycle_decomp(v); for (auto &i : cycles) { if (i.size() > 2) { handle_2(cycles, v); return 0; } } vector<pair<int, int>> tpos; for (auto &i : cycles) if (i.size() > 1) tpos.push_back({i[0], i[1]}); if (tpos.size() == 0) { cout << "0\n"; return 0; } cout << "1\n"; cout << tpos.size() * 2 << '\n'; for (int i = 0; i < tpos.size(); ++i) cout << tpos[i].first+1 << ' '; for (int i = 0; i < tpos.size(); ++i) cout << tpos[tpos.size()-1-i].second+1 << ' '; cout << '\n'; } |