#include <bits/stdc++.h>
using namespace std;
const int NMAX = 3e3;
int n, h[NMAX + 7];
vector<pair<int, int>> zam[10];
int ans(vector<int> t) {
int s = t.size();
if (s == 2) {
zam[1].push_back({t[0], t[1]});
return 1;
}
vector<int> late;
for (int i = 0; i < s; i++) late.push_back(i);
for (int i = 0; i < s - 1; i += 2) {
swap(late[i], late[i + 1]);
}
vector<pair<int, int>> memember;
for (int i = 1; i < s - 1; i += 2) {
memember.push_back({late[i], late[i + 1]});
swap(late[i], late[i + 1]);
}
vector<int> present;
int j = 0;
int odn = late[j];
do {
present.push_back(late[j]);
j = late[j];
} while (late[j] != odn);
reverse(present.begin(), present.end());
// vector<pair<int, int>> _next;
// for (int i = 0; i < s; i++){
// _next.push_back({t[present[i]], present[i]});
// }
for (auto &[a, b] : memember) {
int i = 0, j = 0;
for (int x = 0; x < s; x++) {
if (present[x] == b) {
j = x;
break;
}
}
for (int x = 0; x < s; x++) {
if (present[x] == a) {
i = x;
break;
}
}
zam[1].push_back({t[i], t[j]});
swap(present[i], present[j]);
}
for (int i = 0; i < s; i++) {
if (present[i] & 1) {
for (int j = 0; j < s; j++) {
if (present[j] == present[i] - 1) {
zam[2].push_back({t[i], t[j]});
break;
}
}
}
}
return 2;
}
int go() {
vector<pair<int, int>> t;
for (int i = 0; i < n; i++) {
t.push_back(make_pair(h[i], i));
}
sort(t.begin(), t.end());
int licznik = 0;
vector<int> temp;
for (int i = 0; i < n; i++) {
if (t[i].first == -1) continue;
int j = i;
while (t[j].first != -1) {
temp.push_back(j + 1);
t[j].first = -1;
j = t[j].second;
}
if (temp.size() <= 1){
temp.clear();
continue;
}
licznik = max(ans(temp), licznik);
temp.clear();
}
return licznik;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> h[i];
}
int m = go();
cout << m << "\n";
for (int i = 1; i <= m; i++) {
cout << zam[i].size() * 2 << "\n";
for (int j = 0; j < zam[i].size(); j++) cout << zam[i][j].first << " ";
for (int j = zam[i].size() - 1; j >= 0; j--) cout << zam[i][j].second << " ";
cout << "\n";
}
return 0;
}
// DAWID PAWELEC
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 | #include <bits/stdc++.h> using namespace std; const int NMAX = 3e3; int n, h[NMAX + 7]; vector<pair<int, int>> zam[10]; int ans(vector<int> t) { int s = t.size(); if (s == 2) { zam[1].push_back({t[0], t[1]}); return 1; } vector<int> late; for (int i = 0; i < s; i++) late.push_back(i); for (int i = 0; i < s - 1; i += 2) { swap(late[i], late[i + 1]); } vector<pair<int, int>> memember; for (int i = 1; i < s - 1; i += 2) { memember.push_back({late[i], late[i + 1]}); swap(late[i], late[i + 1]); } vector<int> present; int j = 0; int odn = late[j]; do { present.push_back(late[j]); j = late[j]; } while (late[j] != odn); reverse(present.begin(), present.end()); // vector<pair<int, int>> _next; // for (int i = 0; i < s; i++){ // _next.push_back({t[present[i]], present[i]}); // } for (auto &[a, b] : memember) { int i = 0, j = 0; for (int x = 0; x < s; x++) { if (present[x] == b) { j = x; break; } } for (int x = 0; x < s; x++) { if (present[x] == a) { i = x; break; } } zam[1].push_back({t[i], t[j]}); swap(present[i], present[j]); } for (int i = 0; i < s; i++) { if (present[i] & 1) { for (int j = 0; j < s; j++) { if (present[j] == present[i] - 1) { zam[2].push_back({t[i], t[j]}); break; } } } } return 2; } int go() { vector<pair<int, int>> t; for (int i = 0; i < n; i++) { t.push_back(make_pair(h[i], i)); } sort(t.begin(), t.end()); int licznik = 0; vector<int> temp; for (int i = 0; i < n; i++) { if (t[i].first == -1) continue; int j = i; while (t[j].first != -1) { temp.push_back(j + 1); t[j].first = -1; j = t[j].second; } if (temp.size() <= 1){ temp.clear(); continue; } licznik = max(ans(temp), licznik); temp.clear(); } return licznik; } int32_t main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> h[i]; } int m = go(); cout << m << "\n"; for (int i = 1; i <= m; i++) { cout << zam[i].size() * 2 << "\n"; for (int j = 0; j < zam[i].size(); j++) cout << zam[i][j].first << " "; for (int j = zam[i].size() - 1; j >= 0; j--) cout << zam[i][j].second << " "; cout << "\n"; } return 0; } // DAWID PAWELEC |
English