#include <bits/stdc++.h>
using namespace std;
#ifdef D
#define DEBUG(x) \
do { \
x \
cout.flush(); \
} while (0)
#else
#define DEBUG(x)
#endif
using ll = long long;
const int NMAX = 3e3 + 7;
int n, r;
int t[NMAX];
pair<int, int> s[NMAX];
vector<int> wyjscia, do_pary;
set<int> wset, dpset;
string ans;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> t[i];
s[i] = {t[i], i};
}
sort(s + 1, s + n + 1);
for (int i = 1; i <= n; ++i) {
t[s[i].second] = i;
s[i].first = i;
}
// for (int i = 1; i <= n; ++i) {
// cout << t[i] << " ";
// }
while (true) {
for (int i = n; i > 0; --i) {
if (s[i].second != s[i].first && wset.count(i) == 0 && dpset.count(s[i].second) == 0) {
wyjscia.push_back(s[i].second);
do_pary.push_back(i);
wset.insert(s[i].second);
dpset.insert(i);
}
}
if (wyjscia.empty()) {
break;
}
r++;
for (int i = 0; i < wyjscia.size(); ++i) {
swap(t[wyjscia[i]], t[do_pary[i]]);
}
for (int i = 1; i <= n; ++i) {
s[i] = {t[i], i};
}
sort(s + 1, s + n + 1);
for (int i = 1; i <= n; ++i) {
t[s[i].second] = i;
s[i].first = i;
}
// cout << 2 * wyjscia.size() << "\n";
ans += to_string(2 * wyjscia.size()) + '\n';
for (int e : wyjscia) {
// cout << e << " ";
ans += to_string(e) + ' ';
}
reverse(do_pary.begin(), do_pary.end());
for (int e : do_pary) {
// cout << e << " ";
ans += to_string(e) + ' ';
}
// cout << "\n";
ans += '\n';
wyjscia.clear();
do_pary.clear();
wset.clear();
dpset.clear();
}
cout << r << "\n" << ans;
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | #include <bits/stdc++.h> using namespace std; #ifdef D #define DEBUG(x) \ do { \ x \ cout.flush(); \ } while (0) #else #define DEBUG(x) #endif using ll = long long; const int NMAX = 3e3 + 7; int n, r; int t[NMAX]; pair<int, int> s[NMAX]; vector<int> wyjscia, do_pary; set<int> wset, dpset; string ans; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 1; i <= n; ++i) { cin >> t[i]; s[i] = {t[i], i}; } sort(s + 1, s + n + 1); for (int i = 1; i <= n; ++i) { t[s[i].second] = i; s[i].first = i; } // for (int i = 1; i <= n; ++i) { // cout << t[i] << " "; // } while (true) { for (int i = n; i > 0; --i) { if (s[i].second != s[i].first && wset.count(i) == 0 && dpset.count(s[i].second) == 0) { wyjscia.push_back(s[i].second); do_pary.push_back(i); wset.insert(s[i].second); dpset.insert(i); } } if (wyjscia.empty()) { break; } r++; for (int i = 0; i < wyjscia.size(); ++i) { swap(t[wyjscia[i]], t[do_pary[i]]); } for (int i = 1; i <= n; ++i) { s[i] = {t[i], i}; } sort(s + 1, s + n + 1); for (int i = 1; i <= n; ++i) { t[s[i].second] = i; s[i].first = i; } // cout << 2 * wyjscia.size() << "\n"; ans += to_string(2 * wyjscia.size()) + '\n'; for (int e : wyjscia) { // cout << e << " "; ans += to_string(e) + ' '; } reverse(do_pary.begin(), do_pary.end()); for (int e : do_pary) { // cout << e << " "; ans += to_string(e) + ' '; } // cout << "\n"; ans += '\n'; wyjscia.clear(); do_pary.clear(); wset.clear(); dpset.clear(); } cout << r << "\n" << ans; return 0; } |
English