#include <bits/stdc++.h> #define dbg(...) fprintf(stderr, __VA_ARGS__) #define pb push_back #define ss second #define ff first using namespace std; vector <int> moves(const vector <int> &t) { vector <int> l, r; vector <bool> v(t.size(), 0); for (int i=0; i<(int)t.size(); i++) { if (!v[i]) { v[i] = 1; if (!v[t[i]]) { v[t[i]] = 1; l.pb(i); r.pb(t[i]); } } } while (!r.empty()) { l.pb(r.back()); r.pop_back(); } return l; } void order(vector <int> &t, const vector <int> &p) { vector <int> b; for (int i: p) { b.push_back(t[i]); } for (int i: p) { t[i] = b.back(); b.pop_back(); } } bool sorted(const vector <int> &v) { for (int i=1; i<(int)v.size(); i++) { if (v[i-1] >= v[i]) { return 0; } } return 1; } int main() { int n; scanf("%d", &n); vector <int> t(n); for (int i=0; i<n; i++) scanf("%d", &t[i]); map <int, int> m; for (int i=0; i<n; i++) m[t[i]] = 0; int j = 0; for (pair <int,int> i: m) m[i.ff] = j++; for (int i=0; i<n; i++) t[i] = m[t[i]]; vector <vector <int>> w; while (!sorted(t)) { w.pb(moves(t)); order(t, w.back()); } printf("%lu\n", w.size()); for (vector <int> p: w) { printf("%lu\n", p.size()); for (int i: p) printf("%d ", i+1); printf("\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 | #include <bits/stdc++.h> #define dbg(...) fprintf(stderr, __VA_ARGS__) #define pb push_back #define ss second #define ff first using namespace std; vector <int> moves(const vector <int> &t) { vector <int> l, r; vector <bool> v(t.size(), 0); for (int i=0; i<(int)t.size(); i++) { if (!v[i]) { v[i] = 1; if (!v[t[i]]) { v[t[i]] = 1; l.pb(i); r.pb(t[i]); } } } while (!r.empty()) { l.pb(r.back()); r.pop_back(); } return l; } void order(vector <int> &t, const vector <int> &p) { vector <int> b; for (int i: p) { b.push_back(t[i]); } for (int i: p) { t[i] = b.back(); b.pop_back(); } } bool sorted(const vector <int> &v) { for (int i=1; i<(int)v.size(); i++) { if (v[i-1] >= v[i]) { return 0; } } return 1; } int main() { int n; scanf("%d", &n); vector <int> t(n); for (int i=0; i<n; i++) scanf("%d", &t[i]); map <int, int> m; for (int i=0; i<n; i++) m[t[i]] = 0; int j = 0; for (pair <int,int> i: m) m[i.ff] = j++; for (int i=0; i<n; i++) t[i] = m[t[i]]; vector <vector <int>> w; while (!sorted(t)) { w.pb(moves(t)); order(t, w.back()); } printf("%lu\n", w.size()); for (vector <int> p: w) { printf("%lu\n", p.size()); for (int i: p) printf("%d ", i+1); printf("\n"); } return 0; } |