#include <bits/stdc++.h> using namespace std; bool is_sorted(vector<int> const& v) { for (size_t i = 1; i < v.size(); ++i) { if (v[i - 1] >= v[i]) return false; } return true; } void simulate(vector<int> &p, vector<int> const& rnd) { vector<int> vals; vals.reserve(rnd.size()); for (int pos : rnd) vals.push_back(p[pos - 1]); for (int pos : rnd) { p[pos - 1] = vals.back(); vals.pop_back(); } } bool is_correct(vector<int> p, vector<vector<int>> const& r) { for (auto const &rnd : r) simulate(p, rnd); return is_sorted(p); } void print(vector<int> const& v) { cout << v.size() << '\n'; for (int i : v) cout << i << ' '; cout << '\n'; } using trans_t = pair<int, int>; using transpositions_t = vector<trans_t>; using cycle_t = vector<int>; vector<cycle_t> cycle_decomposition(vector<int> const& p) { vector<cycle_t> r; set<int> not_visited(p.begin(), p.end()); while (not not_visited.empty()) { int s, i; auto beg = not_visited.begin(); s = i = *beg; not_visited.erase(beg); cycle_t c {i}; while (p[i] != s) { i = p[i]; c.push_back(i); not_visited.erase(i); } r.push_back(c); } return r; } pair<vector<int>, vector<int>> unzip(vector<pair<int, int>> const &zipped) { size_t n = zipped.size(); vector<int> fst, snd; fst.reserve(n); snd.reserve(n); for (auto [a, b] : zipped) { fst.push_back(a); snd.push_back(b); } return make_pair(fst, snd); } vector<int> encode_trans(transpositions_t const& ts) { auto [a, b] = unzip(ts); reverse(a.begin(), a.end()); a.insert(a.end(), b.begin(), b.end()); for (int &i : a) ++i; return a; } vector<vector<int>> transpositions(vector<cycle_t> const &cycles) { transpositions_t fst, snd; for (auto const& cycle : cycles) { if (cycle.size() == 2) { trans_t t = make_pair(cycle.front(), cycle.back()); fst.push_back(t); } if (cycle.size() <= 2) continue; // cycle has length greater than 2 int n = cycle.size(); for (int l = 0, r = n - 2; l < r; ) fst.push_back(make_pair(cycle[l++], cycle[r--])); for (int l = 0, r = n - 1; l < r; ) snd.push_back(make_pair(cycle[l++], cycle[r--])); } vector<vector<int>> r; r.reserve(2); vector<int> a = encode_trans(fst), b = encode_trans(snd); if (not a.empty()) r.push_back(a); if (not b.empty()) r.push_back(b); return r; } void sol(vector<int> const& p) { auto init_cycles = cycle_decomposition(p); vector<vector<int>> r = transpositions(init_cycles); // assert(is_correct(p, r)); cout << r.size() << '\n'; for (auto const& v : r) print(v); } // input and setup void solve() { int n; cin >> n; vector<int> a(n); { for (int &i : a) cin >> i; // a is input vector<int> h = a; sort(h.begin(), h.end()); // h = sorted(a) map<int, int> m; for (int i = 0; i < n; ++i) m[h[i]] = i; // m is mapping of values from a onto [0..n-1] for (int &i : a) i = m[i]; } // a is scaled to [0..n-1], can be treated as a permutation sol(a); } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); solve(); 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | #include <bits/stdc++.h> using namespace std; bool is_sorted(vector<int> const& v) { for (size_t i = 1; i < v.size(); ++i) { if (v[i - 1] >= v[i]) return false; } return true; } void simulate(vector<int> &p, vector<int> const& rnd) { vector<int> vals; vals.reserve(rnd.size()); for (int pos : rnd) vals.push_back(p[pos - 1]); for (int pos : rnd) { p[pos - 1] = vals.back(); vals.pop_back(); } } bool is_correct(vector<int> p, vector<vector<int>> const& r) { for (auto const &rnd : r) simulate(p, rnd); return is_sorted(p); } void print(vector<int> const& v) { cout << v.size() << '\n'; for (int i : v) cout << i << ' '; cout << '\n'; } using trans_t = pair<int, int>; using transpositions_t = vector<trans_t>; using cycle_t = vector<int>; vector<cycle_t> cycle_decomposition(vector<int> const& p) { vector<cycle_t> r; set<int> not_visited(p.begin(), p.end()); while (not not_visited.empty()) { int s, i; auto beg = not_visited.begin(); s = i = *beg; not_visited.erase(beg); cycle_t c {i}; while (p[i] != s) { i = p[i]; c.push_back(i); not_visited.erase(i); } r.push_back(c); } return r; } pair<vector<int>, vector<int>> unzip(vector<pair<int, int>> const &zipped) { size_t n = zipped.size(); vector<int> fst, snd; fst.reserve(n); snd.reserve(n); for (auto [a, b] : zipped) { fst.push_back(a); snd.push_back(b); } return make_pair(fst, snd); } vector<int> encode_trans(transpositions_t const& ts) { auto [a, b] = unzip(ts); reverse(a.begin(), a.end()); a.insert(a.end(), b.begin(), b.end()); for (int &i : a) ++i; return a; } vector<vector<int>> transpositions(vector<cycle_t> const &cycles) { transpositions_t fst, snd; for (auto const& cycle : cycles) { if (cycle.size() == 2) { trans_t t = make_pair(cycle.front(), cycle.back()); fst.push_back(t); } if (cycle.size() <= 2) continue; // cycle has length greater than 2 int n = cycle.size(); for (int l = 0, r = n - 2; l < r; ) fst.push_back(make_pair(cycle[l++], cycle[r--])); for (int l = 0, r = n - 1; l < r; ) snd.push_back(make_pair(cycle[l++], cycle[r--])); } vector<vector<int>> r; r.reserve(2); vector<int> a = encode_trans(fst), b = encode_trans(snd); if (not a.empty()) r.push_back(a); if (not b.empty()) r.push_back(b); return r; } void sol(vector<int> const& p) { auto init_cycles = cycle_decomposition(p); vector<vector<int>> r = transpositions(init_cycles); // assert(is_correct(p, r)); cout << r.size() << '\n'; for (auto const& v : r) print(v); } // input and setup void solve() { int n; cin >> n; vector<int> a(n); { for (int &i : a) cin >> i; // a is input vector<int> h = a; sort(h.begin(), h.end()); // h = sorted(a) map<int, int> m; for (int i = 0; i < n; ++i) m[h[i]] = i; // m is mapping of values from a onto [0..n-1] for (int &i : a) i = m[i]; } // a is scaled to [0..n-1], can be treated as a permutation sol(a); } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); solve(); return 0; } |