#include <chrono> #include <cassert> #include <string> #include <array> #include <deque> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <iterator> #include <algorithm> #include <cmath> #include <complex> #include <numeric> #include <random> #include <ios> #include <iostream> #include <bitset> using namespace std; #define fwd(i, a, b) for (int i = (a); i < (b); ++ i) #define rep(i, n) for (int i = 0; i < (n); ++ i) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)x.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define int long long #define ll long long #ifdef LOCAL template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif int32_t main() { fastio; int n; cin >> n; vector<pii> h(n); rep(i, n) { int k; cin >> k; h[i] = make_pair(k, i); } sort(all(h)); vi p(n); rep(i, n) p[h[i].nd] = i; vector<vi> ans; auto fix = [&] () { vi vis(n, -1); vi l, r; rep(i, n) if (vis[i] == -1) { vi cc; int v = i; while (vis[v] == -1) { cc.push_back(v); vis[v] = 1; v = p[v]; } rep(j, sz(cc) / 2) { l.push_back(cc[j]); r.push_back(cc[sz(cc) - 1 - j]); } } if (sz(l) == 0) return; rep(i, sz(l)) swap(p[l[i]], p[r[i]]); ans.push_back(l); reverse(all(r)); for (auto x : r) ans.back().push_back(x); }; fix(); fix(); cout << sz(ans) << '\n'; for (auto l : ans) { cout << sz(l) << '\n'; for (auto x : l) cout << x + 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 107 | #include <chrono> #include <cassert> #include <string> #include <array> #include <deque> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <iterator> #include <algorithm> #include <cmath> #include <complex> #include <numeric> #include <random> #include <ios> #include <iostream> #include <bitset> using namespace std; #define fwd(i, a, b) for (int i = (a); i < (b); ++ i) #define rep(i, n) for (int i = 0; i < (n); ++ i) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)x.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define int long long #define ll long long #ifdef LOCAL template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif int32_t main() { fastio; int n; cin >> n; vector<pii> h(n); rep(i, n) { int k; cin >> k; h[i] = make_pair(k, i); } sort(all(h)); vi p(n); rep(i, n) p[h[i].nd] = i; vector<vi> ans; auto fix = [&] () { vi vis(n, -1); vi l, r; rep(i, n) if (vis[i] == -1) { vi cc; int v = i; while (vis[v] == -1) { cc.push_back(v); vis[v] = 1; v = p[v]; } rep(j, sz(cc) / 2) { l.push_back(cc[j]); r.push_back(cc[sz(cc) - 1 - j]); } } if (sz(l) == 0) return; rep(i, sz(l)) swap(p[l[i]], p[r[i]]); ans.push_back(l); reverse(all(r)); for (auto x : r) ans.back().push_back(x); }; fix(); fix(); cout << sz(ans) << '\n'; for (auto l : ans) { cout << sz(l) << '\n'; for (auto x : l) cout << x + 1 << ' '; cout << '\n'; } return 0; } |