//GRT_2018
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
#define all(x) x.begin(), x.end()
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
const int nax = 3000 + 10;
int n;
pi h[nax];
int p[nax];
bool vis[nax];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> h[i].ST;
h[i].ND = i;
}
sort(h, h + n);
for (int i = 0; i < n; ++i) {
p[h[i].ND] = i;
}
vector<vi>cycles;
int mx = -1;
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
int j = i;
vis[j] = true;
vi cur = {i};
while (!vis[p[j]]) {
vis[p[j]] = true;
cur.PB(p[j]);
j = p[j];
}
mx = max(mx, (int)cur.size());
cycles.PB(cur);
}
}
if (mx == 1) {
cout << "0";
return 0;
} else if (mx == 2) {
cout << "1\n";
vi l = {}, r = {};
for (auto v : cycles) {
if ((int)v.size() == 2) {
l.PB(v[0]);
r.PB(v[1]);
}
}
reverse(all(r));
cout << (int)l.size() * 2 << "\n";
for (int x : l) cout << x + 1 << " ";
for (int x : r) cout << x + 1 << " ";
return 0;
}
cout << "2\n";
vi l = {}, r = {};
for (auto v : cycles) {
if ((int)v.size() > 2) {
int a = 1, b = (int)v.size() - 1;
while (a < b) {
l.PB(v[a]);
r.PB(v[b]);
swap(p[v[a]], p[v[b]]);
a++;
b--;
}
}
}
reverse(all(r));
cout << 2 * (int)l.size() << "\n";
for (int x : l) cout << x + 1 << " ";
for (int x : r) cout << x + 1 << " ";
cout << "\n";
cycles.clear();
for (int i = 0; i < n; ++i) vis[i] = false;
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
int j = i;
vis[j] = true;
vi cur = {i};
while (!vis[p[j]]) {
vis[p[j]] = true;
cur.PB(p[j]);
j = p[j];
}
mx = max(mx, (int)cur.size());
cycles.PB(cur);
}
}
l.clear(); r.clear();
for (auto v : cycles) {
if ((int)v.size() == 2) {
l.PB(v[0]);
r.PB(v[1]);
}
}
cout << 2 * (int)l.size() << "\n";
reverse(all(r));
for (int x : l) cout << x + 1 << " ";
for (int x : r) cout << x + 1 << " ";
}
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 | //GRT_2018 #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define all(x) x.begin(), x.end() //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 3000 + 10; int n; pi h[nax]; int p[nax]; bool vis[nax]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> h[i].ST; h[i].ND = i; } sort(h, h + n); for (int i = 0; i < n; ++i) { p[h[i].ND] = i; } vector<vi>cycles; int mx = -1; for (int i = 0; i < n; ++i) { if (!vis[i]) { int j = i; vis[j] = true; vi cur = {i}; while (!vis[p[j]]) { vis[p[j]] = true; cur.PB(p[j]); j = p[j]; } mx = max(mx, (int)cur.size()); cycles.PB(cur); } } if (mx == 1) { cout << "0"; return 0; } else if (mx == 2) { cout << "1\n"; vi l = {}, r = {}; for (auto v : cycles) { if ((int)v.size() == 2) { l.PB(v[0]); r.PB(v[1]); } } reverse(all(r)); cout << (int)l.size() * 2 << "\n"; for (int x : l) cout << x + 1 << " "; for (int x : r) cout << x + 1 << " "; return 0; } cout << "2\n"; vi l = {}, r = {}; for (auto v : cycles) { if ((int)v.size() > 2) { int a = 1, b = (int)v.size() - 1; while (a < b) { l.PB(v[a]); r.PB(v[b]); swap(p[v[a]], p[v[b]]); a++; b--; } } } reverse(all(r)); cout << 2 * (int)l.size() << "\n"; for (int x : l) cout << x + 1 << " "; for (int x : r) cout << x + 1 << " "; cout << "\n"; cycles.clear(); for (int i = 0; i < n; ++i) vis[i] = false; for (int i = 0; i < n; ++i) { if (!vis[i]) { int j = i; vis[j] = true; vi cur = {i}; while (!vis[p[j]]) { vis[p[j]] = true; cur.PB(p[j]); j = p[j]; } mx = max(mx, (int)cur.size()); cycles.PB(cur); } } l.clear(); r.clear(); for (auto v : cycles) { if ((int)v.size() == 2) { l.PB(v[0]); r.PB(v[1]); } } cout << 2 * (int)l.size() << "\n"; reverse(all(r)); for (int x : l) cout << x + 1 << " "; for (int x : r) cout << x + 1 << " "; } |
English