#include <bits/stdc++.h> //Michał Kuśmirek using namespace std; //XIII LO Szczecin #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- bool check(vector<vector<int>> shifts, vector<int> a) { vector<int> sorted = a; sort(sorted.begin(), sorted.end()); for(vector<int> vec : shifts) { set<int> S; vector<int> values; for(int x : vec) { values.emplace_back(a[x]); S.insert(x); } reverse(values.begin(), values.end()); REP(i, size(vec)) a[vec[i]] = values[i]; assert(size(S) == size(vec)); } return a == sorted; } vector<int> scale(vector<int> a) { int n = size(a); vector<pair<int, int>> sorted(n); REP(i, n) sorted[i] = pair(a[i], i); sort(sorted.begin(), sorted.end()); vector<int> ret(n); REP(i, n) ret[sorted[i].second] = i; return ret; } vector<int> create_round(const vector<int> &a) { int n = size(a); vector<bool> visited(n); vector<vector<int>> cycles; REP(i, n) if(!visited[i]) { int cur = i; cycles.push_back({}); while(!visited[cur]) { visited[cur] = true; cycles.back().emplace_back(cur); cur = a[cur]; } } debug(cycles); vector<int> round; for(vector<int> cycle : cycles) { int len = size(cycle); REP(i, len / 2) round.emplace_back(cycle[i]); } reverse(cycles.begin(), cycles.end()); for(vector<int> cycle : cycles) { int len = size(cycle); FOR(i, len - len / 2, len - 1) round.emplace_back(cycle[i]); } return round; } void make_swaps(vector<int> &a, vector<int> round) { int n = size(round); REP(i, n / 2) swap(a[round[i]], a[round[n - 1 - i]]); } bool sorted(const vector<int> &a) { vector<int> temp = a; sort(temp.begin(), temp.end()); return a == temp; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> input(n); REP(i, n) cin >> input[i]; vector<int> a = scale(input); debug(a); vector<vector<int>> answer; while(!sorted(a)) { vector<int> round = create_round(a); answer.emplace_back(round); make_swaps(a, round); debug(round); debug(a); } cout << size(answer) << '\n'; for(vector<int> round : answer) { cout << size(round) << '\n'; for(int x : round) cout << x + 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 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #include <bits/stdc++.h> //Michał Kuśmirek using namespace std; //XIII LO Szczecin #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define REP(i, n) for(int i = 0; i < (n); ++i) template<class T> int size(T && a) { return (int)(a.size()); } ostream& operator << (ostream &os, string str) { for(char c : str) os << c; return os; } template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << "," << p.second << ')'; } template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) { os << '{'; for(auto it = x.begin(); it != x.end(); ++it) os << *it << (it == prev(x.end()) ? "" : " "); return os << '}'; } template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) { for(auto x : vec) os << "\n " << x; return os; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG # define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n' #else # define debug(...) 0 #endif //-------------------------------------------------- bool check(vector<vector<int>> shifts, vector<int> a) { vector<int> sorted = a; sort(sorted.begin(), sorted.end()); for(vector<int> vec : shifts) { set<int> S; vector<int> values; for(int x : vec) { values.emplace_back(a[x]); S.insert(x); } reverse(values.begin(), values.end()); REP(i, size(vec)) a[vec[i]] = values[i]; assert(size(S) == size(vec)); } return a == sorted; } vector<int> scale(vector<int> a) { int n = size(a); vector<pair<int, int>> sorted(n); REP(i, n) sorted[i] = pair(a[i], i); sort(sorted.begin(), sorted.end()); vector<int> ret(n); REP(i, n) ret[sorted[i].second] = i; return ret; } vector<int> create_round(const vector<int> &a) { int n = size(a); vector<bool> visited(n); vector<vector<int>> cycles; REP(i, n) if(!visited[i]) { int cur = i; cycles.push_back({}); while(!visited[cur]) { visited[cur] = true; cycles.back().emplace_back(cur); cur = a[cur]; } } debug(cycles); vector<int> round; for(vector<int> cycle : cycles) { int len = size(cycle); REP(i, len / 2) round.emplace_back(cycle[i]); } reverse(cycles.begin(), cycles.end()); for(vector<int> cycle : cycles) { int len = size(cycle); FOR(i, len - len / 2, len - 1) round.emplace_back(cycle[i]); } return round; } void make_swaps(vector<int> &a, vector<int> round) { int n = size(round); REP(i, n / 2) swap(a[round[i]], a[round[n - 1 - i]]); } bool sorted(const vector<int> &a) { vector<int> temp = a; sort(temp.begin(), temp.end()); return a == temp; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> input(n); REP(i, n) cin >> input[i]; vector<int> a = scale(input); debug(a); vector<vector<int>> answer; while(!sorted(a)) { vector<int> round = create_round(a); answer.emplace_back(round); make_swaps(a, round); debug(round); debug(a); } cout << size(answer) << '\n'; for(vector<int> round : answer) { cout << size(round) << '\n'; for(int x : round) cout << x + 1 << ' '; cout << '\n'; } } |