#include <iostream> #include <vector> #include <algorithm> using namespace std; bool sortByFirst(const pair<int,int> &a, const pair<int,int> &b) { return a.first < b.first; } vector<int> reverseVectorFull(vector<int> v) { int i = 0, j = v.size() - 1, tmp; while (i < j) { tmp = v[i]; v[i] = v[j]; v[j] = tmp; i++; j--; } // for (int i = 0; i < v.size(); i++) { // cout << v[i] << " "; // } // cout << endl; return v; } vector<int> reverseVector(vector<int> v) { int i = 0, j = v.size() - 2, tmp; while (i < j) { tmp = v[i]; v[i] = v[j]; v[j] = tmp; i++; j--; } // for (int i = 0; i < v.size(); i++) { // cout << v[i] << " "; // } // cout << endl; return v; } int main() { int n, h; cin >> n; int visited[n]; int destination[n]; vector<pair<int, int> > students; for (int i = 0; i < n; i++) { cin >> h;; students.push_back(make_pair(h, i)); } sort(students.begin(), students.end(), sortByFirst); for (int i = 0; i < n; i++) { destination[students[i].second] = i; visited[i] = 0; } vector<vector<int>> cycles; int next; for (int i = 0; i < n; i++) { if (!visited[i]) { vector<int> newCycle; next = destination[i]; newCycle.push_back(i); visited[i] = 1; while (!visited[next]) { visited[next] = 1; newCycle.push_back(next); next = destination[next]; } cycles.push_back(newCycle); } } // for (int i = 0; i < cycles.size(); i++) { // for (int j = 0; j < cycles[i].size(); j++) { // cout << cycles[i][j] << " -> "; // } // cout << endl; // } vector<int> firstLeft, firstRight, secondLeft, secondRight; for (int i = 0; i < cycles.size(); i++) { if (cycles[i].size() == 2) { firstLeft.push_back(cycles[i][0]); firstRight.push_back(cycles[i][1]); } else if (cycles[i].size() > 2) { vector<int> newV = reverseVector(cycles[i]); int l = 0, r = newV.size() - 2, tmp; while (l < r) { firstLeft.push_back(newV[l]); firstRight.push_back(newV[r]); l++; r--; } newV = reverseVectorFull(cycles[i]); l = 0, r = newV.size() - 1; while (l < r) { // secondLeft.push_back(newV[l]); secondLeft.push_back(newV[l]); secondRight.push_back(newV[r]); // secondRight.push_back(); l++; r--; } } } if (secondLeft.size() > 0) { cout << 2 << endl; } else if (firstLeft.size() > 0) { cout << 1 << endl; cout << firstLeft.size() * 2 << endl; for (int i = 0; i < firstLeft.size(); i++) { cout << firstLeft[i] + 1 << " "; } for (int i = firstRight.size() - 1; i >= 0; i--) { cout << firstRight[i] + 1 << " "; } } else { cout << 0; } if (secondLeft.size() > 0) { cout << firstLeft.size() * 2 << endl; for (int i = 0; i < firstLeft.size(); i++) { cout << firstLeft[i] + 1 << " "; } for (int i = firstRight.size() - 1; i >= 0; i--) { cout << firstRight[i] + 1 << " "; } cout << endl; cout << secondLeft.size() * 2 << endl; for (int i = 0; i < secondLeft.size(); i++) { cout << secondLeft[i] + 1 << " "; } for (int i = secondRight.size() - 1; i >= 0; i--) { cout << secondRight[i] + 1 << " "; } cout << endl; } 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | #include <iostream> #include <vector> #include <algorithm> using namespace std; bool sortByFirst(const pair<int,int> &a, const pair<int,int> &b) { return a.first < b.first; } vector<int> reverseVectorFull(vector<int> v) { int i = 0, j = v.size() - 1, tmp; while (i < j) { tmp = v[i]; v[i] = v[j]; v[j] = tmp; i++; j--; } // for (int i = 0; i < v.size(); i++) { // cout << v[i] << " "; // } // cout << endl; return v; } vector<int> reverseVector(vector<int> v) { int i = 0, j = v.size() - 2, tmp; while (i < j) { tmp = v[i]; v[i] = v[j]; v[j] = tmp; i++; j--; } // for (int i = 0; i < v.size(); i++) { // cout << v[i] << " "; // } // cout << endl; return v; } int main() { int n, h; cin >> n; int visited[n]; int destination[n]; vector<pair<int, int> > students; for (int i = 0; i < n; i++) { cin >> h;; students.push_back(make_pair(h, i)); } sort(students.begin(), students.end(), sortByFirst); for (int i = 0; i < n; i++) { destination[students[i].second] = i; visited[i] = 0; } vector<vector<int>> cycles; int next; for (int i = 0; i < n; i++) { if (!visited[i]) { vector<int> newCycle; next = destination[i]; newCycle.push_back(i); visited[i] = 1; while (!visited[next]) { visited[next] = 1; newCycle.push_back(next); next = destination[next]; } cycles.push_back(newCycle); } } // for (int i = 0; i < cycles.size(); i++) { // for (int j = 0; j < cycles[i].size(); j++) { // cout << cycles[i][j] << " -> "; // } // cout << endl; // } vector<int> firstLeft, firstRight, secondLeft, secondRight; for (int i = 0; i < cycles.size(); i++) { if (cycles[i].size() == 2) { firstLeft.push_back(cycles[i][0]); firstRight.push_back(cycles[i][1]); } else if (cycles[i].size() > 2) { vector<int> newV = reverseVector(cycles[i]); int l = 0, r = newV.size() - 2, tmp; while (l < r) { firstLeft.push_back(newV[l]); firstRight.push_back(newV[r]); l++; r--; } newV = reverseVectorFull(cycles[i]); l = 0, r = newV.size() - 1; while (l < r) { // secondLeft.push_back(newV[l]); secondLeft.push_back(newV[l]); secondRight.push_back(newV[r]); // secondRight.push_back(); l++; r--; } } } if (secondLeft.size() > 0) { cout << 2 << endl; } else if (firstLeft.size() > 0) { cout << 1 << endl; cout << firstLeft.size() * 2 << endl; for (int i = 0; i < firstLeft.size(); i++) { cout << firstLeft[i] + 1 << " "; } for (int i = firstRight.size() - 1; i >= 0; i--) { cout << firstRight[i] + 1 << " "; } } else { cout << 0; } if (secondLeft.size() > 0) { cout << firstLeft.size() * 2 << endl; for (int i = 0; i < firstLeft.size(); i++) { cout << firstLeft[i] + 1 << " "; } for (int i = firstRight.size() - 1; i >= 0; i--) { cout << firstRight[i] + 1 << " "; } cout << endl; cout << secondLeft.size() * 2 << endl; for (int i = 0; i < secondLeft.size(); i++) { cout << secondLeft[i] + 1 << " "; } for (int i = secondRight.size() - 1; i >= 0; i--) { cout << secondRight[i] + 1 << " "; } cout << endl; } return 0; } |