#include <iostream> #include <string> #include <algorithm> #include <vector> #define PI pair<int, int> using namespace std; bool check_increasing(int n, vector<int> &arr) { for (int i = 1; i < n; ++i) { if (arr[i] < arr[i - 1]) return false; } return true; } bool check_len(vector<vector<int>> &cycles) { for (vector<int> cycle : cycles) { if (cycle.size() > 2) return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> arr(n); vector<PI> norm(n); for (int i = 0; i < n; ++i) { cin >> arr[i]; norm[i] = {arr[i], i}; } if (check_increasing(n, arr)) { cout << "0"; return 0; } sort(norm.begin(), norm.end()); for (int i = 0; i < n; ++i) arr[norm[i].second] = i + 1; vector<bool> seen(n); vector<vector<int>> cycles; for (int i = 0; i < n; ++i) { if (seen[i] || arr[i] == i + 1) continue; cycles.emplace_back(); cycles.back().push_back(i + 1); seen[i] = true; int j = arr[i] - 1; while (j != i) { cycles.back().push_back(j + 1); seen[j] = true; j = arr[j] - 1; } } if (check_len(cycles)) { cout << "1\n" << 2 * cycles.size() << "\n"; for (vector<int> cycle : cycles) cout << cycle[0] << " "; reverse(cycles.begin(), cycles.end()); for (vector<int> cycle : cycles) cout << cycle[1] << " "; return 0; } vector<PI> first; vector<PI> second; for (vector<int> &cycle : cycles) { reverse(cycle.begin(), cycle.end()); int l = cycle.size(); for (int i = 1; 2 * i < l; ++i) first.emplace_back(cycle[i], cycle[l - i]); for (int i = 0; 2 * i < l - 1; ++i) second.emplace_back(cycle[i], cycle[l - i - 1]); } cout << "2\n" << 2 * first.size() << "\n"; for (PI p : first) cout << p.first << " "; reverse(first.begin(), first.end()); for (PI p : first) cout << p.second << " "; cout << "\n" << 2 * second.size() << "\n"; for (PI p : second) cout << p.first << " "; reverse(second.begin(), second.end()); for (PI p : second) cout << p.second << " "; }
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 | #include <iostream> #include <string> #include <algorithm> #include <vector> #define PI pair<int, int> using namespace std; bool check_increasing(int n, vector<int> &arr) { for (int i = 1; i < n; ++i) { if (arr[i] < arr[i - 1]) return false; } return true; } bool check_len(vector<vector<int>> &cycles) { for (vector<int> cycle : cycles) { if (cycle.size() > 2) return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> arr(n); vector<PI> norm(n); for (int i = 0; i < n; ++i) { cin >> arr[i]; norm[i] = {arr[i], i}; } if (check_increasing(n, arr)) { cout << "0"; return 0; } sort(norm.begin(), norm.end()); for (int i = 0; i < n; ++i) arr[norm[i].second] = i + 1; vector<bool> seen(n); vector<vector<int>> cycles; for (int i = 0; i < n; ++i) { if (seen[i] || arr[i] == i + 1) continue; cycles.emplace_back(); cycles.back().push_back(i + 1); seen[i] = true; int j = arr[i] - 1; while (j != i) { cycles.back().push_back(j + 1); seen[j] = true; j = arr[j] - 1; } } if (check_len(cycles)) { cout << "1\n" << 2 * cycles.size() << "\n"; for (vector<int> cycle : cycles) cout << cycle[0] << " "; reverse(cycles.begin(), cycles.end()); for (vector<int> cycle : cycles) cout << cycle[1] << " "; return 0; } vector<PI> first; vector<PI> second; for (vector<int> &cycle : cycles) { reverse(cycle.begin(), cycle.end()); int l = cycle.size(); for (int i = 1; 2 * i < l; ++i) first.emplace_back(cycle[i], cycle[l - i]); for (int i = 0; 2 * i < l - 1; ++i) second.emplace_back(cycle[i], cycle[l - i - 1]); } cout << "2\n" << 2 * first.size() << "\n"; for (PI p : first) cout << p.first << " "; reverse(first.begin(), first.end()); for (PI p : first) cout << p.second << " "; cout << "\n" << 2 * second.size() << "\n"; for (PI p : second) cout << p.first << " "; reverse(second.begin(), second.end()); for (PI p : second) cout << p.second << " "; } |