#include <bits/stdc++.h> using namespace std; int n, t[3003], k; bool b[3003]; pair < int, int > p[3003]; queue < int > q; stack < int > s; vector < int > v[3003]; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &t[i]); p[i] = { t[i], i }; } sort(p, p + n); for (int i = 0; i < n; ++i) { t[p[i].second] = i; } b[n] = 1; while (b[n]) { b[n] = 0; for (int i = 0; i < n; ++i) { if (t[i] != i && !b[t[i]] && !b[i]) { q.push(t[i]); s.push(i); b[i] = 1; b[t[i]] = 1; b[n] = 1; swap(t[i], t[t[i]]); } } v[k].push_back(2 * s.size()); while (!q.empty()) { v[k].push_back(q.front()); b[q.front()] = 0; q.pop(); } while (!s.empty()) { v[k].push_back(s.top()); b[s.top()] = 0; s.pop(); } ++k; } --k; printf("%d", k); for (int i = 0; i < k; ++i) { printf("\n%d\n", v[i][0]); for (int j = 1; j < v[i].size(); ++j) { printf("%d ", v[i][j] + 1); } } 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 | #include <bits/stdc++.h> using namespace std; int n, t[3003], k; bool b[3003]; pair < int, int > p[3003]; queue < int > q; stack < int > s; vector < int > v[3003]; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &t[i]); p[i] = { t[i], i }; } sort(p, p + n); for (int i = 0; i < n; ++i) { t[p[i].second] = i; } b[n] = 1; while (b[n]) { b[n] = 0; for (int i = 0; i < n; ++i) { if (t[i] != i && !b[t[i]] && !b[i]) { q.push(t[i]); s.push(i); b[i] = 1; b[t[i]] = 1; b[n] = 1; swap(t[i], t[t[i]]); } } v[k].push_back(2 * s.size()); while (!q.empty()) { v[k].push_back(q.front()); b[q.front()] = 0; q.pop(); } while (!s.empty()) { v[k].push_back(s.top()); b[s.top()] = 0; s.pop(); } ++k; } --k; printf("%d", k); for (int i = 0; i < k; ++i) { printf("\n%d\n", v[i][0]); for (int j = 1; j < v[i].size(); ++j) { printf("%d ", v[i][j] + 1); } } return 0; } |