#include <bits/stdc++.h> using namespace std; typedef long long lld; typedef pair<int, int> pii; #define ff first #define dd second #define mp make_pair #define pb push_back constexpr int N = 1 << 12; int p[N], t[N]; pii q[N]; int n, a, b, c; vector<int> q1, q2, q3, q4; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", t + i); q[i] = {t[i], i}; } sort(q, q + n); for (int i = 0; i < n; ++i) { t[q[i].dd] = i; p[i] = q[i].dd; } bool super_git = true; bool git = true; for (int i = 0; i < n; ++i) { if (t[i] != i) super_git = false; if (t[t[i]] != i) { git = false; break; } if (t[p[i]] != i) return 1; } if (super_git) { puts("0"); return 0; } if (git) { printf("1\n"); for (int i = 0; i < n; ++i) { if (t[i] != i) { q1.pb(i); q2.pb(t[i]); swap(t[t[i]], t[i]); } } printf("%zu\n", q1.size() + q2.size()); for (auto e : q1) { printf("%d ", e + 1); } for (auto it = q2.rbegin(); it != q2.rend(); ++it) { printf("%d ", *it + 1); } puts(""); return 0; } puts("2"); for (int i = 0; i < n; ++i) { a = i; b = t[a], c = p[a]; while (b != c) { q1.pb(b); q2.pb(c); swap(p[t[b]], p[t[c]]); swap(t[b], t[c]); a = c; b = t[a], c = p[a]; } } for (int i = 0; i < n; ++i) { if (t[i] != i) { q3.pb(i); q4.pb(t[i]); swap(t[t[i]], t[i]); } } /* puts("t: "); for (int i = 0; i < n; ++i) printf("%d ", t[i]); puts(""); puts("p: "); for (int i = 0; i < n; ++i) printf("%d ", p[i]); puts(""); */ printf("%zu\n", q1.size() + q2.size()); for (auto it = q1.begin(); it != q1.end(); ++it) printf("%d ", *it + 1); for (auto it = q2.rbegin(); it != q2.rend(); ++it) printf("%d ", *it + 1); puts(""); printf("%zu\n", q3.size() + q4.size()); for (auto it = q3.begin(); it != q3.end(); ++it) printf("%d ", *it + 1); for (auto it = q4.rbegin(); it != q4.rend(); ++it) printf("%d ", *it + 1); puts(""); return 0; } /* 5 1670 2011 1560 1232 1447 6 1556 1449 1863 2014 1333 1220 */
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 | #include <bits/stdc++.h> using namespace std; typedef long long lld; typedef pair<int, int> pii; #define ff first #define dd second #define mp make_pair #define pb push_back constexpr int N = 1 << 12; int p[N], t[N]; pii q[N]; int n, a, b, c; vector<int> q1, q2, q3, q4; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", t + i); q[i] = {t[i], i}; } sort(q, q + n); for (int i = 0; i < n; ++i) { t[q[i].dd] = i; p[i] = q[i].dd; } bool super_git = true; bool git = true; for (int i = 0; i < n; ++i) { if (t[i] != i) super_git = false; if (t[t[i]] != i) { git = false; break; } if (t[p[i]] != i) return 1; } if (super_git) { puts("0"); return 0; } if (git) { printf("1\n"); for (int i = 0; i < n; ++i) { if (t[i] != i) { q1.pb(i); q2.pb(t[i]); swap(t[t[i]], t[i]); } } printf("%zu\n", q1.size() + q2.size()); for (auto e : q1) { printf("%d ", e + 1); } for (auto it = q2.rbegin(); it != q2.rend(); ++it) { printf("%d ", *it + 1); } puts(""); return 0; } puts("2"); for (int i = 0; i < n; ++i) { a = i; b = t[a], c = p[a]; while (b != c) { q1.pb(b); q2.pb(c); swap(p[t[b]], p[t[c]]); swap(t[b], t[c]); a = c; b = t[a], c = p[a]; } } for (int i = 0; i < n; ++i) { if (t[i] != i) { q3.pb(i); q4.pb(t[i]); swap(t[t[i]], t[i]); } } /* puts("t: "); for (int i = 0; i < n; ++i) printf("%d ", t[i]); puts(""); puts("p: "); for (int i = 0; i < n; ++i) printf("%d ", p[i]); puts(""); */ printf("%zu\n", q1.size() + q2.size()); for (auto it = q1.begin(); it != q1.end(); ++it) printf("%d ", *it + 1); for (auto it = q2.rbegin(); it != q2.rend(); ++it) printf("%d ", *it + 1); puts(""); printf("%zu\n", q3.size() + q4.size()); for (auto it = q3.begin(); it != q3.end(); ++it) printf("%d ", *it + 1); for (auto it = q4.rbegin(); it != q4.rend(); ++it) printf("%d ", *it + 1); puts(""); return 0; } /* 5 1670 2011 1560 1232 1447 6 1556 1449 1863 2014 1333 1220 */ |