#include <iostream> #include <vector> #include <algorithm> using namespace std; /* 5 1670 2011 1560 1232 1447 6 1556 1449 1863 2014 1333 1220 9 1 5 2 8 9 7 3 6 4 8 7 2 5 8 6 4 2 1 * */ struct Person { int height; int startIndex; int targetIndex; bool processed; }; bool person_sorter(Person const &lp, Person const &rp) { return lp.height < rp.height; } bool canBeSolvedInOneRound(int n, Person *initPersons) { for (int i = 0; i < n; i++) { if (initPersons[initPersons[i].targetIndex].targetIndex != i) { return false; } } return true; } void solveInitialRound(int n, Person *persons, vector<int> &a, vector<int> &b) { for (int i = 0; i < n; i++) { int index1 = i; int index2 = persons[persons[i].targetIndex].targetIndex; //whaaaat? if (persons[i].targetIndex != persons[i].startIndex && !persons[index1].processed && !persons[index2].processed && index2 != i) { a.push_back(index1); b.push_back(index2); persons[index1].processed = true; persons[index2].processed = true; Person p1 = persons[index1]; Person p2 = persons[index2]; persons[index1] = p2; persons[index2] = p1; persons[index1].startIndex = index1; persons[index2].startIndex = index2; } } for (int i = 0; i < n; i++) { persons[i].processed = false; } } void printResults(vector<vector<int> > vec2) { for(int v = 0; v < vec2.size(); v+=2) { vector<int> a = vec2[v]; vector<int> b = vec2[v+1]; printf("%lu\n", a.size() * 2); for (int i = 0; i < a.size(); i++) { printf("%d ", a[i] + 1); } for (int i = b.size() - 1; i >= 0; i--) { printf("%d ", b[i] + 1); } printf("\n"); } } void solveFinalRound(int n, Person *persons) { vector<int> a; vector<int> b; for (int i = 0; i < n; i++) { if (persons[i].targetIndex != persons[i].startIndex && !persons[i].processed && !persons[persons[i].targetIndex].processed) { //TODO check second processed flag a.push_back(persons[i].targetIndex); b.push_back(persons[i].startIndex); persons[i].processed = true; persons[persons[i].targetIndex].processed = true; } } // reverse(b.begin(), b.end()); printf("%lu\n", a.size() * 2); for (int i = 0; i < a.size(); i++) { printf("%d ", a[i] + 1); } for (int i = b.size() - 1; i >= 0; i--) { printf("%d ", b[i] + 1); } printf("\n"); } int main() { int n, h; scanf("%d", &n); Person persons[n]; Person initPositionsPersons[n]; for (int i = 0; i < n; i++) { scanf("%d", &h); persons[i].height = h; persons[i].startIndex = i; initPositionsPersons[i].height = h; initPositionsPersons[i].startIndex = i; initPositionsPersons[i].processed = false; } std::sort(persons, persons + n, person_sorter); bool isSorted = true; for (int i = 0; i < n; i++) { persons[i].targetIndex = i; initPositionsPersons[persons[i].startIndex].targetIndex = i; if (persons[i].startIndex != persons[i].targetIndex) { isSorted = false; } } if (isSorted) { ::printf("0\n"); return 0; } int rounds = 1; vector<vector<int> > vec2; while (!canBeSolvedInOneRound(n, initPositionsPersons)) { vector<int> a; vector<int> b; solveInitialRound(n, initPositionsPersons, a, b); rounds++; vec2.push_back(a); vec2.push_back(b); } printf("%d\n", rounds); printResults(vec2); solveFinalRound(n, initPositionsPersons); 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #include <iostream> #include <vector> #include <algorithm> using namespace std; /* 5 1670 2011 1560 1232 1447 6 1556 1449 1863 2014 1333 1220 9 1 5 2 8 9 7 3 6 4 8 7 2 5 8 6 4 2 1 * */ struct Person { int height; int startIndex; int targetIndex; bool processed; }; bool person_sorter(Person const &lp, Person const &rp) { return lp.height < rp.height; } bool canBeSolvedInOneRound(int n, Person *initPersons) { for (int i = 0; i < n; i++) { if (initPersons[initPersons[i].targetIndex].targetIndex != i) { return false; } } return true; } void solveInitialRound(int n, Person *persons, vector<int> &a, vector<int> &b) { for (int i = 0; i < n; i++) { int index1 = i; int index2 = persons[persons[i].targetIndex].targetIndex; //whaaaat? if (persons[i].targetIndex != persons[i].startIndex && !persons[index1].processed && !persons[index2].processed && index2 != i) { a.push_back(index1); b.push_back(index2); persons[index1].processed = true; persons[index2].processed = true; Person p1 = persons[index1]; Person p2 = persons[index2]; persons[index1] = p2; persons[index2] = p1; persons[index1].startIndex = index1; persons[index2].startIndex = index2; } } for (int i = 0; i < n; i++) { persons[i].processed = false; } } void printResults(vector<vector<int> > vec2) { for(int v = 0; v < vec2.size(); v+=2) { vector<int> a = vec2[v]; vector<int> b = vec2[v+1]; printf("%lu\n", a.size() * 2); for (int i = 0; i < a.size(); i++) { printf("%d ", a[i] + 1); } for (int i = b.size() - 1; i >= 0; i--) { printf("%d ", b[i] + 1); } printf("\n"); } } void solveFinalRound(int n, Person *persons) { vector<int> a; vector<int> b; for (int i = 0; i < n; i++) { if (persons[i].targetIndex != persons[i].startIndex && !persons[i].processed && !persons[persons[i].targetIndex].processed) { //TODO check second processed flag a.push_back(persons[i].targetIndex); b.push_back(persons[i].startIndex); persons[i].processed = true; persons[persons[i].targetIndex].processed = true; } } // reverse(b.begin(), b.end()); printf("%lu\n", a.size() * 2); for (int i = 0; i < a.size(); i++) { printf("%d ", a[i] + 1); } for (int i = b.size() - 1; i >= 0; i--) { printf("%d ", b[i] + 1); } printf("\n"); } int main() { int n, h; scanf("%d", &n); Person persons[n]; Person initPositionsPersons[n]; for (int i = 0; i < n; i++) { scanf("%d", &h); persons[i].height = h; persons[i].startIndex = i; initPositionsPersons[i].height = h; initPositionsPersons[i].startIndex = i; initPositionsPersons[i].processed = false; } std::sort(persons, persons + n, person_sorter); bool isSorted = true; for (int i = 0; i < n; i++) { persons[i].targetIndex = i; initPositionsPersons[persons[i].startIndex].targetIndex = i; if (persons[i].startIndex != persons[i].targetIndex) { isSorted = false; } } if (isSorted) { ::printf("0\n"); return 0; } int rounds = 1; vector<vector<int> > vec2; while (!canBeSolvedInOneRound(n, initPositionsPersons)) { vector<int> a; vector<int> b; solveInitialRound(n, initPositionsPersons, a, b); rounds++; vec2.push_back(a); vec2.push_back(b); } printf("%d\n", rounds); printResults(vec2); solveFinalRound(n, initPositionsPersons); return 0; } |