#include <bits/stdc++.h> using namespace std; int length; int *values = new int[length]; vector<int> changes1begin; vector<int> changes1end; vector<int> changes2begin; vector<int> changes2end; void readAndNormalize() { cin >> length; int *sorted = new int[length]; for (int i = 0; i < length; ++i) { cin >> values[i]; sorted[i] = values[i]; } sort(sorted, sorted + length); unordered_map<int, int> sortedMap; for (int i = 0; i < length; ++i) { sortedMap[sorted[i]] = i; } for (int i = 0; i < length; ++i) { values[i] = sortedMap[values[i]]; } } bool isSorted() { for (int i = 0; i < length-1; ++i) { if (values[i] >= values[i+1]) { return false; } } return true; } vector<int> getCycle(int index) { vector<int> result; result.push_back(values[index]); int current = values[index]; while (current != index) { result.push_back(values[current]); current = values[current]; } return result; } vector<vector<int>> getCycles() { vector<vector<int>> cycles; unordered_set<int> elems; for (int i = 0; i < length; ++i) { if (elems.find(i) == elems.end()) { vector<int> cycle = getCycle(i); for (int j = 0; j < cycle.size(); ++j) { elems.insert(cycle[j]); } cycles.push_back(cycle); } } return cycles; } void changeCycle(vector<int> cycle) { if (cycle.size() == 2) { changes1begin.push_back(cycle[0]); changes1end.push_back(cycle[1]); } else { for (int i = 0; i < (cycle.size()-1) / 2; ++i) { changes1begin.push_back(cycle[i+1]); changes1end.push_back(cycle[cycle.size()-1-i]); } for (int i = 0; i < cycle.size() / 2; ++i) { changes2begin.push_back(cycle[i+1]); changes2end.push_back(cycle[(cycle.size() - i) % cycle.size()]); } } } void printChanges(vector<int> changesBegin, vector<int> changesEnd) { cout << changesBegin.size() * 2 << endl; for (int i = 0; i < changesBegin.size(); ++i) { cout << changesBegin[i]+1 << " "; } for (int i = changesEnd.size() - 1; i >= 0; --i) { cout << changesEnd[i]+1; if (i > 0) { cout << " "; } } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); readAndNormalize(); if (isSorted()) { cout << 0 << endl; return 0; } vector<vector<int>> cycles = getCycles(); for (int i = 0; i < cycles.size(); ++i) { changeCycle(cycles[i]); } if (changes2begin.size() > 0) { cout << 2 << endl; printChanges(changes1begin, changes1end); printChanges(changes2begin, changes2end); } else { cout << 1 << endl; printChanges(changes1begin, changes1end); } 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #include <bits/stdc++.h> using namespace std; int length; int *values = new int[length]; vector<int> changes1begin; vector<int> changes1end; vector<int> changes2begin; vector<int> changes2end; void readAndNormalize() { cin >> length; int *sorted = new int[length]; for (int i = 0; i < length; ++i) { cin >> values[i]; sorted[i] = values[i]; } sort(sorted, sorted + length); unordered_map<int, int> sortedMap; for (int i = 0; i < length; ++i) { sortedMap[sorted[i]] = i; } for (int i = 0; i < length; ++i) { values[i] = sortedMap[values[i]]; } } bool isSorted() { for (int i = 0; i < length-1; ++i) { if (values[i] >= values[i+1]) { return false; } } return true; } vector<int> getCycle(int index) { vector<int> result; result.push_back(values[index]); int current = values[index]; while (current != index) { result.push_back(values[current]); current = values[current]; } return result; } vector<vector<int>> getCycles() { vector<vector<int>> cycles; unordered_set<int> elems; for (int i = 0; i < length; ++i) { if (elems.find(i) == elems.end()) { vector<int> cycle = getCycle(i); for (int j = 0; j < cycle.size(); ++j) { elems.insert(cycle[j]); } cycles.push_back(cycle); } } return cycles; } void changeCycle(vector<int> cycle) { if (cycle.size() == 2) { changes1begin.push_back(cycle[0]); changes1end.push_back(cycle[1]); } else { for (int i = 0; i < (cycle.size()-1) / 2; ++i) { changes1begin.push_back(cycle[i+1]); changes1end.push_back(cycle[cycle.size()-1-i]); } for (int i = 0; i < cycle.size() / 2; ++i) { changes2begin.push_back(cycle[i+1]); changes2end.push_back(cycle[(cycle.size() - i) % cycle.size()]); } } } void printChanges(vector<int> changesBegin, vector<int> changesEnd) { cout << changesBegin.size() * 2 << endl; for (int i = 0; i < changesBegin.size(); ++i) { cout << changesBegin[i]+1 << " "; } for (int i = changesEnd.size() - 1; i >= 0; --i) { cout << changesEnd[i]+1; if (i > 0) { cout << " "; } } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); readAndNormalize(); if (isSorted()) { cout << 0 << endl; return 0; } vector<vector<int>> cycles = getCycles(); for (int i = 0; i < cycles.size(); ++i) { changeCycle(cycles[i]); } if (changes2begin.size() > 0) { cout << 2 << endl; printChanges(changes1begin, changes1end); printChanges(changes2begin, changes2end); } else { cout << 1 << endl; printChanges(changes1begin, changes1end); } return 0; } |