#include "bits/stdc++.h" // Ignacy Boehlke using namespace std; // XIII LO Szczecin template<class A,class B>ostream&operator<<(ostream&os,pair<A,B>p){return os<<'{'<<p.first<<", "<<p.second<<'}';} template<class T>auto operator<<(ostream&os,T v)->decltype(v.end(),os){os<<'{';int i=0;for(auto e:v)os<<(", ")+2*!i++<<e;return os<<'}';} #ifdef DEBUG #define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define LOG(...)(void)0 #endif #define ssize(x)((int)x.size()) #define FOR(a,b,c)for(int a=(b);a<=(c);a++) #define REP(a,b)FOR(a,0,(b)-1) #define ALL(x)(x).begin(), (x).end() #define fi first #define se second using ll=long long; vector<int> conv(const vector<pair<int, int>>& swaps) { int m = ssize(swaps); vector<int> res(2 * m); REP(i, m) res[i] = swaps[i].fi; REP(i, m) res[m + i] = swaps[m - 1 - i].se; return res; } int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector<pair<int, int>> vec(n); REP(i, n) { vec[i].fi = i; cin >> vec[i].se; } sort(ALL(vec), [](const auto& a, const auto& b){return a.se < b.se;}); REP(i, n) vec[i].se = i; sort(ALL(vec)); vector<int> h(n); REP(i, n) h[i] = vec[i].se; LOG(h); vector<vector<int>> cycles; vector<bool> vis(n); bool normal = false; REP(i, n) if (!vis[i]) { vis[i] = true; if (h[i] == i) continue; cycles.emplace_back(); cycles.back().push_back(i); int v = h[i]; while (!vis[v]) { vis[v] = true; cycles.back().push_back(v); v = h[v]; } if (ssize(cycles.back()) > 2) normal = true; } LOG(cycles); vector<vector<int>> res; if (!cycles.empty()) { vector<pair<int, int>> swaps; if (normal) { for (const vector<int>& cyc : cycles) { for (int i = 0, j = ssize(cyc) - 2; i < j; i++, j--) { swaps.emplace_back(cyc[i], cyc[j]); swap(h[cyc[i]], h[cyc[j]]); } } res.emplace_back(conv(swaps)); } LOG(swaps); swaps.clear(); LOG(h); REP(i, n) if (h[i] > i) swaps.emplace_back(i, h[i]); LOG(swaps); res.emplace_back(conv(swaps)); } cout << ssize(res) << '\n'; REP(i, ssize(res)) { cout << ssize(res[i]) << '\n'; REP(j, ssize(res[i])) cout << res[i][j] + 1 << ' '; cout << '\n'; } }
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 | #include "bits/stdc++.h" // Ignacy Boehlke using namespace std; // XIII LO Szczecin template<class A,class B>ostream&operator<<(ostream&os,pair<A,B>p){return os<<'{'<<p.first<<", "<<p.second<<'}';} template<class T>auto operator<<(ostream&os,T v)->decltype(v.end(),os){os<<'{';int i=0;for(auto e:v)os<<(", ")+2*!i++<<e;return os<<'}';} #ifdef DEBUG #define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define LOG(...)(void)0 #endif #define ssize(x)((int)x.size()) #define FOR(a,b,c)for(int a=(b);a<=(c);a++) #define REP(a,b)FOR(a,0,(b)-1) #define ALL(x)(x).begin(), (x).end() #define fi first #define se second using ll=long long; vector<int> conv(const vector<pair<int, int>>& swaps) { int m = ssize(swaps); vector<int> res(2 * m); REP(i, m) res[i] = swaps[i].fi; REP(i, m) res[m + i] = swaps[m - 1 - i].se; return res; } int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector<pair<int, int>> vec(n); REP(i, n) { vec[i].fi = i; cin >> vec[i].se; } sort(ALL(vec), [](const auto& a, const auto& b){return a.se < b.se;}); REP(i, n) vec[i].se = i; sort(ALL(vec)); vector<int> h(n); REP(i, n) h[i] = vec[i].se; LOG(h); vector<vector<int>> cycles; vector<bool> vis(n); bool normal = false; REP(i, n) if (!vis[i]) { vis[i] = true; if (h[i] == i) continue; cycles.emplace_back(); cycles.back().push_back(i); int v = h[i]; while (!vis[v]) { vis[v] = true; cycles.back().push_back(v); v = h[v]; } if (ssize(cycles.back()) > 2) normal = true; } LOG(cycles); vector<vector<int>> res; if (!cycles.empty()) { vector<pair<int, int>> swaps; if (normal) { for (const vector<int>& cyc : cycles) { for (int i = 0, j = ssize(cyc) - 2; i < j; i++, j--) { swaps.emplace_back(cyc[i], cyc[j]); swap(h[cyc[i]], h[cyc[j]]); } } res.emplace_back(conv(swaps)); } LOG(swaps); swaps.clear(); LOG(h); REP(i, n) if (h[i] > i) swaps.emplace_back(i, h[i]); LOG(swaps); res.emplace_back(conv(swaps)); } cout << ssize(res) << '\n'; REP(i, ssize(res)) { cout << ssize(res[i]) << '\n'; REP(j, ssize(res[i])) cout << res[i][j] + 1 << ' '; cout << '\n'; } } |