#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template<typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template<typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 3007; int n, A[N], I[N]; bool vis[N]; bool is_sorted() { for (int i = 1; i + 1 <= n; i++) { if (A[i] > A[i + 1]) return false; } return true; } int main() { _upgrade cin >> n; for (int i = 1; i <= n; i++) cin >> A[i]; map<int, int> mp; for (int i = 1; i <= n; i++) mp[A[i]] = 1; int cnt = 1; for (auto &x: mp) x.second = cnt++; for (int i = 1; i <= n; i++) { A[i] = mp[A[i]]; I[A[i]] = i; } vector<vector<int>> ans; int ccnt = 0; while (++ccnt < 10 && !is_sorted()) { for (int i = 1; i <= n; i++) { vis[i] = false; } vector<int> l, r; for (int i = 1; i <= n; i++) { if (!vis[i]) { int pos = i, want = i; while (!vis[I[want]] && A[pos] != want) { l.push_back(pos); r.push_back(I[want]); vis[pos] = true; vis[I[want]] = true; pos = A[pos]; want = I[want]; } } } ans.push_back(l); for (int i = sz(r) - 1; i >= 0; i--) ans.back().push_back(r[i]); for (int i = 0; i < sz(l); i++) { swap(I[A[l[i]]], I[A[r[i]]]); swap(A[l[i]], A[r[i]]); } } cout << sz(ans) << "\n"; for (auto &v: ans) { cout << sz(v) << "\n"; for (auto x: v) cout << x << " "; 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 108 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template<typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template<typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 3007; int n, A[N], I[N]; bool vis[N]; bool is_sorted() { for (int i = 1; i + 1 <= n; i++) { if (A[i] > A[i + 1]) return false; } return true; } int main() { _upgrade cin >> n; for (int i = 1; i <= n; i++) cin >> A[i]; map<int, int> mp; for (int i = 1; i <= n; i++) mp[A[i]] = 1; int cnt = 1; for (auto &x: mp) x.second = cnt++; for (int i = 1; i <= n; i++) { A[i] = mp[A[i]]; I[A[i]] = i; } vector<vector<int>> ans; int ccnt = 0; while (++ccnt < 10 && !is_sorted()) { for (int i = 1; i <= n; i++) { vis[i] = false; } vector<int> l, r; for (int i = 1; i <= n; i++) { if (!vis[i]) { int pos = i, want = i; while (!vis[I[want]] && A[pos] != want) { l.push_back(pos); r.push_back(I[want]); vis[pos] = true; vis[I[want]] = true; pos = A[pos]; want = I[want]; } } } ans.push_back(l); for (int i = sz(r) - 1; i >= 0; i--) ans.back().push_back(r[i]); for (int i = 0; i < sz(l); i++) { swap(I[A[l[i]]], I[A[r[i]]]); swap(A[l[i]], A[r[i]]); } } cout << sz(ans) << "\n"; for (auto &v: ans) { cout << sz(v) << "\n"; for (auto x: v) cout << x << " "; cout << "\n"; } return 0; } |