#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <cassert>
using namespace std;
// #define DEBUG
#ifdef DEBUG
#define dassert(x) assert(x);
#define df(...) printf(__VA_ARGS__)
#else
#define dassert(x)
#define df(...)
#endif
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define ir(x, a, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define sz(x) (ll)x.size()
#define foru(i, n) for (int i = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
typedef int64_t ll;
vec<bool> used;
vec<int> a;
vec<vec<int>> cycles;
void cycle(int v) {
if (used[v]) return;
df("--%d", v);
cycles.back().pb(v);
used[v] = 1;
cycle(a[v]);
}
bool sorted(const vec<int>& a) {
foru (i, a.size()) if (a[i] != i) return 0;
return 1;
}
int main() {
df("debug mode\n");
#ifndef DEBUG
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
int n;
cin >> n;
vec<pair<int, int>> b(n);
used.resize(n);
a.resize(n);
foru (i, n) {
cin >> b[i].x;
b[i].y = i;
}
sort(all(b));
foru (i, n) a[b[i].y] = i;
for (auto x : a) { df("%d ", x); }
df("\n");
vec<vec<pair<int, int>>> moves;
if (!sorted(a)) {
moves.pb({});
foru (i, n) {
if (!used[i]) { cycles.pb({}); df("cycle"); cycle(i); df("\n"); }
}
for (auto& cyc : cycles) {
// cout << cyc.size() << endl;
if (cyc.size() <= 1) continue;
if (cyc.size() == 2) {
moves.back().pb({cyc[0], cyc[1]});
swap(a[cyc[0]], a[cyc[1]]);
continue;
}
for (int i = 1; i < (cyc.size()+1)/2; ++i) {
df("%d--%d\n", i, cyc.size()-i);
moves.back().pb({cyc[i], cyc[cyc.size()-i]});
swap(a[cyc[i]], a[cyc[cyc.size()-i]]);
}
}
}
// cout << endl;
if (!sorted(a)) {
moves.pb({});
foru (i, n) {
if (a[i] != i) {
assert(a[a[i]] == i);
moves.back().pb({i, a[i]});
swap(a[i], a[a[i]]);
}
}
}
assert(sorted(a));
cout << moves.size() << "\n";
for (auto& v : moves) {
cout << 2*v.size() << "\n";
foru (i, v.size()) cout << v[i].x+1 << " ";
for (int i = v.size()-1; i >= 0; --i) cout << v[i].y+1 << " ";
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #include <iostream> #include <algorithm> #include <vector> #include <numeric> #include <cassert> using namespace std; // #define DEBUG #ifdef DEBUG #define dassert(x) assert(x); #define df(...) printf(__VA_ARGS__) #else #define dassert(x) #define df(...) #endif #define x first #define y second #define mp make_pair #define pb push_back #define ir(x, a, b) ((a) <= (x) && (x) <= (b)) #define vec vector #define sz(x) (ll)x.size() #define foru(i, n) for (int i = 0; (i) < (n); ++(i)) #define all(x) (x).begin(), (x).end() typedef int64_t ll; vec<bool> used; vec<int> a; vec<vec<int>> cycles; void cycle(int v) { if (used[v]) return; df("--%d", v); cycles.back().pb(v); used[v] = 1; cycle(a[v]); } bool sorted(const vec<int>& a) { foru (i, a.size()) if (a[i] != i) return 0; return 1; } int main() { df("debug mode\n"); #ifndef DEBUG ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #endif int n; cin >> n; vec<pair<int, int>> b(n); used.resize(n); a.resize(n); foru (i, n) { cin >> b[i].x; b[i].y = i; } sort(all(b)); foru (i, n) a[b[i].y] = i; for (auto x : a) { df("%d ", x); } df("\n"); vec<vec<pair<int, int>>> moves; if (!sorted(a)) { moves.pb({}); foru (i, n) { if (!used[i]) { cycles.pb({}); df("cycle"); cycle(i); df("\n"); } } for (auto& cyc : cycles) { // cout << cyc.size() << endl; if (cyc.size() <= 1) continue; if (cyc.size() == 2) { moves.back().pb({cyc[0], cyc[1]}); swap(a[cyc[0]], a[cyc[1]]); continue; } for (int i = 1; i < (cyc.size()+1)/2; ++i) { df("%d--%d\n", i, cyc.size()-i); moves.back().pb({cyc[i], cyc[cyc.size()-i]}); swap(a[cyc[i]], a[cyc[cyc.size()-i]]); } } } // cout << endl; if (!sorted(a)) { moves.pb({}); foru (i, n) { if (a[i] != i) { assert(a[a[i]] == i); moves.back().pb({i, a[i]}); swap(a[i], a[a[i]]); } } } assert(sorted(a)); cout << moves.size() << "\n"; for (auto& v : moves) { cout << 2*v.size() << "\n"; foru (i, v.size()) cout << v[i].x+1 << " "; for (int i = v.size()-1; i >= 0; --i) cout << v[i].y+1 << " "; cout << "\n"; } return 0; } |
English