#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 50;
int n, a[N], b[N], vis[N];
vector<vector<int>> cycs;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
b[i] = a[i];
}
if (is_sorted(a + 1, a + n + 1)) {
cout << 0 << '\n';
return 0;
}
sort(b + 1, b + n + 1);
for (int i = 1; i <= n; ++i) {
a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;
}
bool ok = true;
for (int i = 1; i <= n; ++i) {
int x = i, c = 0;
if (vis[x]) continue;
vector<int> cyc;
while (!vis[x]) {
vis[x] = true;
cyc.push_back(x);
x = a[x];
++c;
}
if (c > 2) ok = false;
cycs.push_back(cyc);
}
if (!ok) {
cout << 2 << '\n';
vector<int> ll, rr;
for (auto &vec : cycs) {
if (vec.size() > 2) {
for (int x = 1; x < (vec.size() + 1) / 2; ++x) {
int l = x, r = ((int)(vec.size()) - x);
swap(a[vec[l]], a[vec[r]]);
ll.push_back(vec[l]);
rr.push_back(vec[r]);
}
}
}
int tot = ll.size() + rr.size();
cout << tot << '\n';
for (int i = 0; i < (int)ll.size(); ++i)
cout << ll[i] << ' ';
for (int i = (int)(rr.size()) - 1; i >= 0; --i)
cout << rr[i] << ' ';
cout << '\n';
}
else {
cout << 1 << '\n';
}
vector<int> ll, rr;
for (int i = 1; i <= n; ++i) {
assert(i == a[i] || i == a[a[i]]);
if (i != a[i] && i < a[i]) {
ll.push_back(i);
rr.push_back(a[i]);
}
}
int tot = ll.size() + rr.size();
cout << tot << '\n';
for (int i = 0; i < (int)ll.size(); ++i)
cout << ll[i] << ' ';
for (int i = (int)(rr.size()) - 1; i >= 0; --i)
cout << rr[i] << ' ';
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 | #include <bits/stdc++.h> using namespace std; const int N = 1e6 + 50; int n, a[N], b[N], vis[N]; vector<vector<int>> cycs; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; b[i] = a[i]; } if (is_sorted(a + 1, a + n + 1)) { cout << 0 << '\n'; return 0; } sort(b + 1, b + n + 1); for (int i = 1; i <= n; ++i) { a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b; } bool ok = true; for (int i = 1; i <= n; ++i) { int x = i, c = 0; if (vis[x]) continue; vector<int> cyc; while (!vis[x]) { vis[x] = true; cyc.push_back(x); x = a[x]; ++c; } if (c > 2) ok = false; cycs.push_back(cyc); } if (!ok) { cout << 2 << '\n'; vector<int> ll, rr; for (auto &vec : cycs) { if (vec.size() > 2) { for (int x = 1; x < (vec.size() + 1) / 2; ++x) { int l = x, r = ((int)(vec.size()) - x); swap(a[vec[l]], a[vec[r]]); ll.push_back(vec[l]); rr.push_back(vec[r]); } } } int tot = ll.size() + rr.size(); cout << tot << '\n'; for (int i = 0; i < (int)ll.size(); ++i) cout << ll[i] << ' '; for (int i = (int)(rr.size()) - 1; i >= 0; --i) cout << rr[i] << ' '; cout << '\n'; } else { cout << 1 << '\n'; } vector<int> ll, rr; for (int i = 1; i <= n; ++i) { assert(i == a[i] || i == a[a[i]]); if (i != a[i] && i < a[i]) { ll.push_back(i); rr.push_back(a[i]); } } int tot = ll.size() + rr.size(); cout << tot << '\n'; for (int i = 0; i < (int)ll.size(); ++i) cout << ll[i] << ' '; for (int i = (int)(rr.size()) - 1; i >= 0; --i) cout << rr[i] << ' '; cout << '\n'; return 0; } |
English