//2022-12-15 //author: Marcin Rolbiecki #include <bits/stdc++.h> using namespace std; const int maxN = 3e3+10; int n, r; int H[maxN], sortpos[maxN], pos[maxN]; int pom[maxN]; bool odw[maxN]; int namnie[maxN]; int k[3]; vector <vector<int>> out (maxN); int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> H[i]; pom[i] = H[i]; pos[ H[i] ] = i; } if (is_sorted(H+1, H+n+1)) { cout << 0; return 0; } sort (pom + 1, pom + 1 + n); for (int i = 1; i <= n; i++) { sortpos[ pom[i] ] = i; namnie[i] = pos[pom[i]]; } int maxdepth = 1; for (int i = 1; i <= n; i++) { int h = H[i]; int depth = 1; while (!odw[h]) { odw[h] = 1; int p = H[sortpos[h]]; if(!odw[p]) depth++; h = p; } maxdepth = max(maxdepth, depth); } fill (odw, odw + maxN, 0); if (maxdepth > 2) { r++; deque <int> przod, tyl; for (int i = 1; i <= n; i++) { int h = H[i]; int l = i; while (sortpos[h] != namnie[l]) { int a = sortpos[h], b = namnie[l]; if (odw[a] || odw[b]) break; przod.push_back(a); odw[a] = 1; tyl.push_front(b); odw[b] = 1; namnie[sortpos[H[a]]] = b; namnie[l] = a; if (namnie[b] == a) namnie[b] = b; swap(H[a], H[b]); l = b; h = H[l]; } } k[r] = przod.size() + tyl.size(); for (int it : przod) out[r].push_back(it); for (int it : tyl) out[r].push_back(it); } r++; deque <int> przod, tyl; for (int i = 1; i <= n; i++) { if (i != sortpos[H[i]]) { przod.push_back(i); tyl.push_front(sortpos[H[i]]); swap(H[i], H[sortpos[H[i]]]); } } k[r] = przod.size() + tyl.size(); for (int it : przod) out[r].push_back(it); for (int it : tyl) out[r].push_back(it); cout << r << '\n'; for (int i = 1; i <= r; i++) { cout << k[i] << '\n'; for (int it : out[i]) cout << it << ' '; cout << '\n'; } 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 | //2022-12-15 //author: Marcin Rolbiecki #include <bits/stdc++.h> using namespace std; const int maxN = 3e3+10; int n, r; int H[maxN], sortpos[maxN], pos[maxN]; int pom[maxN]; bool odw[maxN]; int namnie[maxN]; int k[3]; vector <vector<int>> out (maxN); int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> H[i]; pom[i] = H[i]; pos[ H[i] ] = i; } if (is_sorted(H+1, H+n+1)) { cout << 0; return 0; } sort (pom + 1, pom + 1 + n); for (int i = 1; i <= n; i++) { sortpos[ pom[i] ] = i; namnie[i] = pos[pom[i]]; } int maxdepth = 1; for (int i = 1; i <= n; i++) { int h = H[i]; int depth = 1; while (!odw[h]) { odw[h] = 1; int p = H[sortpos[h]]; if(!odw[p]) depth++; h = p; } maxdepth = max(maxdepth, depth); } fill (odw, odw + maxN, 0); if (maxdepth > 2) { r++; deque <int> przod, tyl; for (int i = 1; i <= n; i++) { int h = H[i]; int l = i; while (sortpos[h] != namnie[l]) { int a = sortpos[h], b = namnie[l]; if (odw[a] || odw[b]) break; przod.push_back(a); odw[a] = 1; tyl.push_front(b); odw[b] = 1; namnie[sortpos[H[a]]] = b; namnie[l] = a; if (namnie[b] == a) namnie[b] = b; swap(H[a], H[b]); l = b; h = H[l]; } } k[r] = przod.size() + tyl.size(); for (int it : przod) out[r].push_back(it); for (int it : tyl) out[r].push_back(it); } r++; deque <int> przod, tyl; for (int i = 1; i <= n; i++) { if (i != sortpos[H[i]]) { przod.push_back(i); tyl.push_front(sortpos[H[i]]); swap(H[i], H[sortpos[H[i]]]); } } k[r] = przod.size() + tyl.size(); for (int it : przod) out[r].push_back(it); for (int it : tyl) out[r].push_back(it); cout << r << '\n'; for (int i = 1; i <= r; i++) { cout << k[i] << '\n'; for (int it : out[i]) cout << it << ' '; cout << '\n'; } return 0; } |