#include <bits/stdc++.h> typedef long long int lli; using namespace std; #define MAX4 10010 #define MAX5 100010 #define MAX6 1000010 template <typename T > void dbg(T last) { cout << last << "\n"; } template <typename T, typename ...args> void dbg(T current, args ...next) { cout << current << ' '; dbg(next...); } //-----------------------------------------------------------------------------------------------// int tab[MAX4]; int stab[MAX4]; int org[MAX4]; map<int, int> heightToidx; map<int, int> currentPos; vector<int> ans[MAX4]; int n; bool checkOrder() { for (int i=0;i<n;i++) { if (stab[i] != tab[i]) { return false; } } return true; } bool isVisited(int x, set<int> &s) { return s.find(x) != s.end(); } void solve() { cin >> n; for (int i=0;i<n;i++) { cin >> tab[i]; stab[i] = tab[i]; currentPos[tab[i]] = i; org[i] = tab[i]; } sort(stab, stab + n); for (int i=0;i<n;i++) { heightToidx[stab[i]] = i; } bool sorted = checkOrder(); int rounds = 0; set<int> visited; deque<int> swaps; while (!sorted) { vector<vector<int>> perm; for (int i=0;i<n;i++) { if (tab[i] != stab[i] && !isVisited(i, visited)) { int x = heightToidx[tab[i]]; perm.push_back(vector<int>()); perm.back().push_back(i); visited.insert(i); while (!isVisited(x, visited)) { visited.insert(x); perm.back().push_back(x); x = heightToidx[tab[x]]; } } } /* 8 2 3 7 8 1 4 5 6 */ // for (int i=0;i<perm.size();i++) // { // for (int j=0;j<perm[i].size();j++) // { // cout << perm[i][j] << " "; // } // cout << endl; // } // cout << "*\n"; // for (int i=0;i<n;i++) // { // cout << tab[i] << " "; // } // cout << "\n**\n"; for (int i=0;i<perm.size();i++) { for (int j=1;j<perm[i].size();j+=2) { // dbg(perm[i][j-1]+ 1, perm[i][j] + 1); swaps.push_back(perm[i][j-1]); swaps.push_front(perm[i][j]); } } for (int x : swaps) { ans[rounds].push_back(x); } while (!swaps.empty()) { int x = swaps.front(); int y = swaps.back(); swaps.pop_front(); swaps.pop_back(); swap(tab[x], tab[y]); } visited.clear(); rounds++; sorted = checkOrder(); } cout << rounds << "\n"; for (int i=0;i<rounds;i++) { cout << ans[i].size() << "\n"; for (int x : ans[i]) { cout << x + 1 << " "; } cout << "\n"; // set <int> ll; // for (int x : ans[i]) // { // if (ll.find(x) != ll.end()) // { // cout << "blad"; // exit(-1); // } // ll.insert(x); // } } } int main() { std::ios::sync_with_stdio(false); solve(); 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 | #include <bits/stdc++.h> typedef long long int lli; using namespace std; #define MAX4 10010 #define MAX5 100010 #define MAX6 1000010 template <typename T > void dbg(T last) { cout << last << "\n"; } template <typename T, typename ...args> void dbg(T current, args ...next) { cout << current << ' '; dbg(next...); } //-----------------------------------------------------------------------------------------------// int tab[MAX4]; int stab[MAX4]; int org[MAX4]; map<int, int> heightToidx; map<int, int> currentPos; vector<int> ans[MAX4]; int n; bool checkOrder() { for (int i=0;i<n;i++) { if (stab[i] != tab[i]) { return false; } } return true; } bool isVisited(int x, set<int> &s) { return s.find(x) != s.end(); } void solve() { cin >> n; for (int i=0;i<n;i++) { cin >> tab[i]; stab[i] = tab[i]; currentPos[tab[i]] = i; org[i] = tab[i]; } sort(stab, stab + n); for (int i=0;i<n;i++) { heightToidx[stab[i]] = i; } bool sorted = checkOrder(); int rounds = 0; set<int> visited; deque<int> swaps; while (!sorted) { vector<vector<int>> perm; for (int i=0;i<n;i++) { if (tab[i] != stab[i] && !isVisited(i, visited)) { int x = heightToidx[tab[i]]; perm.push_back(vector<int>()); perm.back().push_back(i); visited.insert(i); while (!isVisited(x, visited)) { visited.insert(x); perm.back().push_back(x); x = heightToidx[tab[x]]; } } } /* 8 2 3 7 8 1 4 5 6 */ // for (int i=0;i<perm.size();i++) // { // for (int j=0;j<perm[i].size();j++) // { // cout << perm[i][j] << " "; // } // cout << endl; // } // cout << "*\n"; // for (int i=0;i<n;i++) // { // cout << tab[i] << " "; // } // cout << "\n**\n"; for (int i=0;i<perm.size();i++) { for (int j=1;j<perm[i].size();j+=2) { // dbg(perm[i][j-1]+ 1, perm[i][j] + 1); swaps.push_back(perm[i][j-1]); swaps.push_front(perm[i][j]); } } for (int x : swaps) { ans[rounds].push_back(x); } while (!swaps.empty()) { int x = swaps.front(); int y = swaps.back(); swaps.pop_front(); swaps.pop_back(); swap(tab[x], tab[y]); } visited.clear(); rounds++; sorted = checkOrder(); } cout << rounds << "\n"; for (int i=0;i<rounds;i++) { cout << ans[i].size() << "\n"; for (int x : ans[i]) { cout << x + 1 << " "; } cout << "\n"; // set <int> ll; // for (int x : ans[i]) // { // if (ll.find(x) != ll.end()) // { // cout << "blad"; // exit(-1); // } // ll.insert(x); // } } } int main() { std::ios::sync_with_stdio(false); solve(); return 0; } |