#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = b; i >= a; i--) #define cat(x) cout << #x << ": " << x << endl using namespace std; using ll = long long; const int N = 3030; vector<vector<int>> res; int n, a[N], b[N], want[N], vis[N]; int next(int i) { return want[a[i]]; } void add(vector<pair<int, int>> v) { if (v.empty()) return; for (auto [x, y] : v) swap(a[x], a[y]); vector<int> op; for (auto [x, y] : v) op.push_back(x); reverse(v.begin(), v.end()); for (auto [x, y] : v) op.push_back(y); res.push_back(op); } void pairs() { vector<pair<int, int>> cur; rep(i, 1, n) { if (next(next(i)) != i) { return; } if (next(i) != i && i < next(i)) { cur.push_back({i, next(i)}); } } add(cur); rep(i, 1, n) assert(next(i) == i); assert(res.size() <= 2); cout << res.size() << "\n"; for (auto op : res) { cout << op.size() << "\n"; for (auto x : op) { cout << x << " "; } cout << "\n"; } exit(0); } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n; rep(i, 1, n) cin >> a[i]; rep(i, 1, n) b[i] = a[i]; sort(b + 1, b + n + 1); rep(i, 1, n) want[b[i]] = i; pairs(); vector<pair<int, int>> cur; rep(i, 1, n) { if (vis[i]) continue; vector<int> v; int x = i; while (!vis[x]) { vis[x] = 1; v.push_back(x); x = next(x); } for (int i = 1; i < int(v.size()) - i; i++) { cur.push_back({v[i], v[v.size() - i]}); } } add(cur); pairs(); assert(0); 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 | #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = b; i >= a; i--) #define cat(x) cout << #x << ": " << x << endl using namespace std; using ll = long long; const int N = 3030; vector<vector<int>> res; int n, a[N], b[N], want[N], vis[N]; int next(int i) { return want[a[i]]; } void add(vector<pair<int, int>> v) { if (v.empty()) return; for (auto [x, y] : v) swap(a[x], a[y]); vector<int> op; for (auto [x, y] : v) op.push_back(x); reverse(v.begin(), v.end()); for (auto [x, y] : v) op.push_back(y); res.push_back(op); } void pairs() { vector<pair<int, int>> cur; rep(i, 1, n) { if (next(next(i)) != i) { return; } if (next(i) != i && i < next(i)) { cur.push_back({i, next(i)}); } } add(cur); rep(i, 1, n) assert(next(i) == i); assert(res.size() <= 2); cout << res.size() << "\n"; for (auto op : res) { cout << op.size() << "\n"; for (auto x : op) { cout << x << " "; } cout << "\n"; } exit(0); } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n; rep(i, 1, n) cin >> a[i]; rep(i, 1, n) b[i] = a[i]; sort(b + 1, b + n + 1); rep(i, 1, n) want[b[i]] = i; pairs(); vector<pair<int, int>> cur; rep(i, 1, n) { if (vis[i]) continue; vector<int> v; int x = i; while (!vis[x]) { vis[x] = 1; v.push_back(x); x = next(x); } for (int i = 1; i < int(v.size()) - i; i++) { cur.push_back({v[i], v[v.size() - i]}); } } add(cur); pairs(); assert(0); return 0; } |