// C3 - Fotografia #include <iostream> #include <vector> #include <algorithm> #include <deque> using namespace std; struct Person { int height; int id; int sortPos; int currPos; bool marked; Person() { } Person(int h, int p) { height = h; id = p; currPos = p; } bool operator<(const Person& P) { return this->height < P.height; } }; int main() { ios_base::sync_with_stdio(0); int n, h; Person *people; int *currState; vector<deque<int>> result; vector<int> loners; bool sorted; int currNode; int destNode; vector<vector<int>> cycles; vector<int> currCycle; int piv1, piv2; cin >> n; people = new Person[n]; currState = new int[n]; for (int i = 0; i < n; ++i) { cin >> h; people[i] = Person(h, i); } sort(people, people + n); for (int i = 0; i < n; ++i) { people[i].sortPos = i; currState[people[i].currPos] = people[i].sortPos; } while(true) { for (int i = 0; i < n; ++i) { people[i].marked = false; } cycles.clear(); for (int i = 0; i < n; ++i) { if (people[i].marked == false) { currCycle.clear(); currNode = i; while (people[currNode].marked == false) { people[currNode].marked = true; currCycle.push_back(currNode); currNode = currState[people[currNode].sortPos]; } if (currCycle.size() > 1) { cycles.push_back(currCycle); } } } if (cycles.size() == 0) { break; } result.push_back(deque<int>()); for (unsigned i = 0; i < cycles.size(); ++i) { piv1 = cycles[i].size() / 4; piv2 = 3 * cycles[i].size() / 4; result[result.size() - 1].push_front(people[cycles[i][piv1]].currPos); result[result.size() - 1].push_back(people[cycles[i][piv2]].currPos); swap(currState[people[cycles[i][piv1]].currPos], currState[people[cycles[i][piv2]].currPos]); swap(people[cycles[i][piv1]].currPos, people[cycles[i][piv2]].currPos); if (piv2 - piv1 - 1 >= 2 ) { currCycle.clear(); currCycle.insert(currCycle.end(), cycles[i].begin() + piv1 + 1, cycles[i].begin() + piv2); cycles.push_back(currCycle); } if (piv1 + cycles[i].size() - piv2 - 1 >= 2) { currCycle.clear(); currCycle.insert(currCycle.end(), cycles[i].begin() + piv2 + 1, cycles[i].end()); currCycle.insert(currCycle.end(), cycles[i].begin(), cycles[i].begin() + piv1); cycles.push_back(currCycle); } } } /*while (true) { sorted = true; for (int i = 0; i < n; ++i) { if (currState[i] != i) { sorted = false; } people[i].marked = false; } if (sorted) { break; } result.push_back(deque<int>()); for (int i = 0; i < n; ++i) { if (!people[i].marked && people[i].sortPos != people[i].currPos) { currNode = i; while (true) { if (people[currNode].marked) { break; } people[currNode].marked = true; destNode = currState[currNode]; if (!people[destNode].marked) { people[destNode].marked = true; result[result.size() - 1].push_front(people[currNode].currPos); result[result.size() - 1].push_back(people[destNode].currPos); swap(currState[people[currNode].currPos], currState[people[destNode].currPos]); swap(people[currNode].currPos, people[destNode].currPos); currNode = currState[people[destNode].sortPos]; } else { break; } } } } }*/ cout << result.size() << endl; for (unsigned i = 0; i < result.size(); ++i) { cout << result[i].size() << endl; for (deque<int>::iterator it = result[i].begin(); it != result[i].end(); ++it) { cout << *it + 1 << " "; } cout << endl; } delete [] people; delete [] currState; 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 | // C3 - Fotografia #include <iostream> #include <vector> #include <algorithm> #include <deque> using namespace std; struct Person { int height; int id; int sortPos; int currPos; bool marked; Person() { } Person(int h, int p) { height = h; id = p; currPos = p; } bool operator<(const Person& P) { return this->height < P.height; } }; int main() { ios_base::sync_with_stdio(0); int n, h; Person *people; int *currState; vector<deque<int>> result; vector<int> loners; bool sorted; int currNode; int destNode; vector<vector<int>> cycles; vector<int> currCycle; int piv1, piv2; cin >> n; people = new Person[n]; currState = new int[n]; for (int i = 0; i < n; ++i) { cin >> h; people[i] = Person(h, i); } sort(people, people + n); for (int i = 0; i < n; ++i) { people[i].sortPos = i; currState[people[i].currPos] = people[i].sortPos; } while(true) { for (int i = 0; i < n; ++i) { people[i].marked = false; } cycles.clear(); for (int i = 0; i < n; ++i) { if (people[i].marked == false) { currCycle.clear(); currNode = i; while (people[currNode].marked == false) { people[currNode].marked = true; currCycle.push_back(currNode); currNode = currState[people[currNode].sortPos]; } if (currCycle.size() > 1) { cycles.push_back(currCycle); } } } if (cycles.size() == 0) { break; } result.push_back(deque<int>()); for (unsigned i = 0; i < cycles.size(); ++i) { piv1 = cycles[i].size() / 4; piv2 = 3 * cycles[i].size() / 4; result[result.size() - 1].push_front(people[cycles[i][piv1]].currPos); result[result.size() - 1].push_back(people[cycles[i][piv2]].currPos); swap(currState[people[cycles[i][piv1]].currPos], currState[people[cycles[i][piv2]].currPos]); swap(people[cycles[i][piv1]].currPos, people[cycles[i][piv2]].currPos); if (piv2 - piv1 - 1 >= 2 ) { currCycle.clear(); currCycle.insert(currCycle.end(), cycles[i].begin() + piv1 + 1, cycles[i].begin() + piv2); cycles.push_back(currCycle); } if (piv1 + cycles[i].size() - piv2 - 1 >= 2) { currCycle.clear(); currCycle.insert(currCycle.end(), cycles[i].begin() + piv2 + 1, cycles[i].end()); currCycle.insert(currCycle.end(), cycles[i].begin(), cycles[i].begin() + piv1); cycles.push_back(currCycle); } } } /*while (true) { sorted = true; for (int i = 0; i < n; ++i) { if (currState[i] != i) { sorted = false; } people[i].marked = false; } if (sorted) { break; } result.push_back(deque<int>()); for (int i = 0; i < n; ++i) { if (!people[i].marked && people[i].sortPos != people[i].currPos) { currNode = i; while (true) { if (people[currNode].marked) { break; } people[currNode].marked = true; destNode = currState[currNode]; if (!people[destNode].marked) { people[destNode].marked = true; result[result.size() - 1].push_front(people[currNode].currPos); result[result.size() - 1].push_back(people[destNode].currPos); swap(currState[people[currNode].currPos], currState[people[destNode].currPos]); swap(people[currNode].currPos, people[destNode].currPos); currNode = currState[people[destNode].sortPos]; } else { break; } } } } }*/ cout << result.size() << endl; for (unsigned i = 0; i < result.size(); ++i) { cout << result[i].size() << endl; for (deque<int>::iterator it = result[i].begin(); it != result[i].end(); ++it) { cout << *it + 1 << " "; } cout << endl; } delete [] people; delete [] currState; return 0; } |