// Grzegorz Ryn // Jagiellonian University #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 pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define st first #define nd second #define all(__VAR) (__VAR).begin(), (__VAR).end() #define DEBUG(__VAR) cout << #__VAR << ": " << __VAR << endl; template <typename T, typename U> ostream & operator<<(ostream &out, const pair<T, U> &var) { cout << "{" << var.st << ", " << var.nd << "}"; return out; } template <typename T> ostream & operator<<(ostream &out, const vector<T> &var) { cout << "["; for(int i=0; i<(int)var.size()-1; i++) { cout << var[i] << ", "; } if(var.size()>0) cout << var.back(); cout << "]" << endl; return out; } const int inf = 1'000'000'000; void scale(vector<int>& a) { int N = a.size(); vector<pii> order; for(int i=0; i<N; i++) { order.pb({a[i], i}); } sort(order.begin(), order.end()); for(int i=0; i<N; i++) { a[order[i].nd] = i; } } int get_cnt(vector<int>& a) { int N = a.size(), cnt = 0; for(int i=0; i<N; i++) { if(a[i] != i) { cnt++; } } return cnt; } void update(vector<int>& h, deque<int> operation) { int M = operation.size(); vector<int> v(M); for(int i=0; i<M; i++) { v[i] = h[operation[i]]; } reverse(v.begin(), v.end()); for(int i=0; i<M; i++) { h[operation[i]] = v[i]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N; cin >> N; vector<int> h(N); for(int i=0; i<N; i++) { cin >> h[i]; } scale(h); vector<deque<int>> result; while(get_cnt(h) > 0) { //cout << "XD: " << h; vector<bool> vis(N, false); deque<int> operation; for(int i=0; i<N; i++) { if(vis[i]) { continue; } vector<int> cycle; int j = i; while(vis[j] == false) { vis[j] = true; cycle.pb(j); j = h[j]; } int M = cycle.size(); int a=0, b=M-1; while(a<b) { operation.push_front(cycle[a++]); operation.push_back(cycle[b--]); } } update(h, operation); result.push_back(operation); } cout << result.size() << '\n'; for(auto v:result) { cout << v.size() << '\n'; for(int i:v) { cout << i+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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | // Grzegorz Ryn // Jagiellonian University #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 pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define st first #define nd second #define all(__VAR) (__VAR).begin(), (__VAR).end() #define DEBUG(__VAR) cout << #__VAR << ": " << __VAR << endl; template <typename T, typename U> ostream & operator<<(ostream &out, const pair<T, U> &var) { cout << "{" << var.st << ", " << var.nd << "}"; return out; } template <typename T> ostream & operator<<(ostream &out, const vector<T> &var) { cout << "["; for(int i=0; i<(int)var.size()-1; i++) { cout << var[i] << ", "; } if(var.size()>0) cout << var.back(); cout << "]" << endl; return out; } const int inf = 1'000'000'000; void scale(vector<int>& a) { int N = a.size(); vector<pii> order; for(int i=0; i<N; i++) { order.pb({a[i], i}); } sort(order.begin(), order.end()); for(int i=0; i<N; i++) { a[order[i].nd] = i; } } int get_cnt(vector<int>& a) { int N = a.size(), cnt = 0; for(int i=0; i<N; i++) { if(a[i] != i) { cnt++; } } return cnt; } void update(vector<int>& h, deque<int> operation) { int M = operation.size(); vector<int> v(M); for(int i=0; i<M; i++) { v[i] = h[operation[i]]; } reverse(v.begin(), v.end()); for(int i=0; i<M; i++) { h[operation[i]] = v[i]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N; cin >> N; vector<int> h(N); for(int i=0; i<N; i++) { cin >> h[i]; } scale(h); vector<deque<int>> result; while(get_cnt(h) > 0) { //cout << "XD: " << h; vector<bool> vis(N, false); deque<int> operation; for(int i=0; i<N; i++) { if(vis[i]) { continue; } vector<int> cycle; int j = i; while(vis[j] == false) { vis[j] = true; cycle.pb(j); j = h[j]; } int M = cycle.size(); int a=0, b=M-1; while(a<b) { operation.push_front(cycle[a++]); operation.push_back(cycle[b--]); } } update(h, operation); result.push_back(operation); } cout << result.size() << '\n'; for(auto v:result) { cout << v.size() << '\n'; for(int i:v) { cout << i+1 << ' '; } cout << '\n'; } return 0; } |