#include <iostream> #include <utility> #include <string> #include <vector> #include <algorithm> #include <queue> using namespace std; const int mxN = 3000; vector<int> c[mxN]; pair<int, int> p[mxN]; pair<int, int> P[mxN]; bool byl[mxN]; int mx = 0, n; vector<int> stack; queue<int> q; void czysc() { for (int i = 0; i < n; i++) byl[i] = false; return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; p[i] = { x, i }; P[i] = { x, i }; } sort(p, p + n); for (int i = 0; i < n; i++) P[p[i].second].second = i; /*int nr = 0; for (int i = 0; i < n; i++) if(!byl[i]) { c[nr].push_back(i); byl[i] = true; int size = 1; int iZ = p[i].second; while (i != iZ) { c[nr].push_back(iZ); byl[iZ] = true; size++; iZ = p[iZ].second; } mx = max(size, mx); nr++; } if (mx == 1) { cout << 0; return 0; } cout << mx / 2 + mx % 2 << '\n';*/ bool rob = true; int odp = 0; string s = ""; while (rob) { czysc(); rob = false; int ile = 0; for (int i = 0; i < n; i++) if (P[i].second != i && !byl[i] && !byl[P[i].second]) { byl[i] = true; byl[P[i].second] = true; ile += 2; q.push(i); stack.push_back(P[i].second); rob = true; swap(P[i], P[P[i].second]); } if (ile == 0) break; odp++; s += to_string(ile); s += '\n'; while (!q.empty()) { s += to_string(q.front() + 1); s += ' '; q.pop(); } while (!stack.empty()) { s += to_string(stack.back() + 1); s += ' '; stack.pop_back(); } s += '\n'; } cout << odp << '\n' << s; 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 | #include <iostream> #include <utility> #include <string> #include <vector> #include <algorithm> #include <queue> using namespace std; const int mxN = 3000; vector<int> c[mxN]; pair<int, int> p[mxN]; pair<int, int> P[mxN]; bool byl[mxN]; int mx = 0, n; vector<int> stack; queue<int> q; void czysc() { for (int i = 0; i < n; i++) byl[i] = false; return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; p[i] = { x, i }; P[i] = { x, i }; } sort(p, p + n); for (int i = 0; i < n; i++) P[p[i].second].second = i; /*int nr = 0; for (int i = 0; i < n; i++) if(!byl[i]) { c[nr].push_back(i); byl[i] = true; int size = 1; int iZ = p[i].second; while (i != iZ) { c[nr].push_back(iZ); byl[iZ] = true; size++; iZ = p[iZ].second; } mx = max(size, mx); nr++; } if (mx == 1) { cout << 0; return 0; } cout << mx / 2 + mx % 2 << '\n';*/ bool rob = true; int odp = 0; string s = ""; while (rob) { czysc(); rob = false; int ile = 0; for (int i = 0; i < n; i++) if (P[i].second != i && !byl[i] && !byl[P[i].second]) { byl[i] = true; byl[P[i].second] = true; ile += 2; q.push(i); stack.push_back(P[i].second); rob = true; swap(P[i], P[P[i].second]); } if (ile == 0) break; odp++; s += to_string(ile); s += '\n'; while (!q.empty()) { s += to_string(q.front() + 1); s += ' '; q.pop(); } while (!stack.empty()) { s += to_string(stack.back() + 1); s += ' '; stack.pop_back(); } s += '\n'; } cout << odp << '\n' << s; return 0; } |