#include <iostream> #include <vector> #include <algorithm> #include <set> #include <map> #include <cmath> #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) using namespace std; const int MAX_N = 3e3; const int LUCKY_NUM = 13; int n, ith, currentCycle, result, maxLength, len; int height[MAX_N + LUCKY_NUM]; int permutation[MAX_N + LUCKY_NUM]; bool visited[MAX_N + LUCKY_NUM]; map<int, int> heightsMap; vector<vector<int>> cycles; vector<int> lft, rgt; void DFS(int v, int cycleId) { visited[v] = true; cycles[cycleId].push_back(v); if (!visited[permutation[v]]) { DFS(permutation[v], cycleId); } } void printSortingRound() { cout << lft.size() + rgt.size() << endl; for (auto pos: lft) { cout << pos << " "; } for (auto pos: rgt) { cout << pos << " "; } cout << endl; } void endOfSortingRound() { reverse(rgt.begin(), rgt.end()); printSortingRound(); lft.clear(); rgt.clear(); } void firstSortingRound() { for (auto &cycle: cycles) { len = (int) cycle.size(); if (len == 2) { lft.push_back(cycle[0]); rgt.push_back(cycle[1]); } else { for (int i = 1; i <= (len - 1) / 2; i++) { lft.push_back(cycle[i]); } for (int i = len - 1; i >= len / 2 + 1; i--) { rgt.push_back(cycle[i]); } } } endOfSortingRound(); } void secondSortingRound() { for (auto &cycle: cycles) { len = (int) cycle.size(); if (len != 2) { for (int i = 1; i <= len / 2; i++) { lft.push_back(cycle[i]); } rgt.push_back(cycle[0]); for (int i = len - 1; i > len / 2 + (len % 2 == 1); i--) { rgt.push_back(cycle[i]); } } } endOfSortingRound(); } void findMaxLength() { for (const auto &cycle: cycles) { maxLength = max(maxLength, (int) cycle.size()); } } void buildPermutation() { for (int i = 1; i <= n; i++) { heightsMap[height[i]] = i; } for (auto it = heightsMap.begin(); it != heightsMap.end(); it++) { ith = (int) distance(heightsMap.begin(), it) + 1; permutation[it->second] = ith; } } void buildCycles() { for (int i = 1; i <= n; i++) { if (!visited[i] && permutation[i] != i) { cycles.emplace_back(); DFS(i, currentCycle++); } } } void readInput() { boost; cin >> n; for (int i = 1; i <= n; i++) { cin >> height[i]; } } void solve() { buildPermutation(); buildCycles(); findMaxLength(); result = maxLength == 2 ? 1 : (maxLength == 0 ? 0 : 2); cout << result << endl; if (result == 0) return; firstSortingRound(); if (result == 1) return; secondSortingRound(); } int32_t main() { readInput(); solve(); }
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 | #include <iostream> #include <vector> #include <algorithm> #include <set> #include <map> #include <cmath> #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) using namespace std; const int MAX_N = 3e3; const int LUCKY_NUM = 13; int n, ith, currentCycle, result, maxLength, len; int height[MAX_N + LUCKY_NUM]; int permutation[MAX_N + LUCKY_NUM]; bool visited[MAX_N + LUCKY_NUM]; map<int, int> heightsMap; vector<vector<int>> cycles; vector<int> lft, rgt; void DFS(int v, int cycleId) { visited[v] = true; cycles[cycleId].push_back(v); if (!visited[permutation[v]]) { DFS(permutation[v], cycleId); } } void printSortingRound() { cout << lft.size() + rgt.size() << endl; for (auto pos: lft) { cout << pos << " "; } for (auto pos: rgt) { cout << pos << " "; } cout << endl; } void endOfSortingRound() { reverse(rgt.begin(), rgt.end()); printSortingRound(); lft.clear(); rgt.clear(); } void firstSortingRound() { for (auto &cycle: cycles) { len = (int) cycle.size(); if (len == 2) { lft.push_back(cycle[0]); rgt.push_back(cycle[1]); } else { for (int i = 1; i <= (len - 1) / 2; i++) { lft.push_back(cycle[i]); } for (int i = len - 1; i >= len / 2 + 1; i--) { rgt.push_back(cycle[i]); } } } endOfSortingRound(); } void secondSortingRound() { for (auto &cycle: cycles) { len = (int) cycle.size(); if (len != 2) { for (int i = 1; i <= len / 2; i++) { lft.push_back(cycle[i]); } rgt.push_back(cycle[0]); for (int i = len - 1; i > len / 2 + (len % 2 == 1); i--) { rgt.push_back(cycle[i]); } } } endOfSortingRound(); } void findMaxLength() { for (const auto &cycle: cycles) { maxLength = max(maxLength, (int) cycle.size()); } } void buildPermutation() { for (int i = 1; i <= n; i++) { heightsMap[height[i]] = i; } for (auto it = heightsMap.begin(); it != heightsMap.end(); it++) { ith = (int) distance(heightsMap.begin(), it) + 1; permutation[it->second] = ith; } } void buildCycles() { for (int i = 1; i <= n; i++) { if (!visited[i] && permutation[i] != i) { cycles.emplace_back(); DFS(i, currentCycle++); } } } void readInput() { boost; cin >> n; for (int i = 1; i <= n; i++) { cin >> height[i]; } } void solve() { buildPermutation(); buildCycles(); findMaxLength(); result = maxLength == 2 ? 1 : (maxLength == 0 ? 0 : 2); cout << result << endl; if (result == 0) return; firstSortingRound(); if (result == 1) return; secondSortingRound(); } int32_t main() { readInput(); solve(); } |