#ifndef LOCAL #pragma GCC optimize("O3") #endif #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define mor > muu & operator << ( #define R22(r) sim > typename \ enable_if<1 r sizeof dud<c>(0), muu&>::type operator<<(c g) { sim > struct rge {c b, e; }; sim > rge<c> range(c h, c n) { return {h, n}; } sim > auto dud(c * r) -> decltype(cerr << *r); sim > char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() { cerr << a.str() << endl; } R22(<) a << boolalpha << g; ris; } R22(==) ris << range(begin(g), end(g)); } sim, class b mor pair < b, c > r) { ris << "(" << r.first << ", " << r.second << ")"; } sim mor rge<c> u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } #else sim mor const c&) { ris; } #endif muu & operator()() { ris; } }; #define imie(r...) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ") vector<bool> vis; vector<int> graph; vector<vector<int>> cycles; void dfs(int x) { if (vis[x]) return; vis[x] = true; cycles.back().push_back(x); dfs(graph[x]); } void find_cycles(int x) { if (vis[x]) return; cycles.push_back({}); dfs(x); } void append_swap(deque<int>& sol, int a, int b) { sol.push_front(a); sol.push_back(b); } void print_sol(vector<deque<int>> v) { // assert(v.size() <= 2); cout << v.size() << "\n"; for (auto shuffle : v) { cout << shuffle.size() << "\n"; for (int x : shuffle) { cout << x << " "; } cout << "\n"; } } void solve1(int n) { deque<int> sol; for (auto cycle : cycles) { if (cycle.size() == 1) continue; append_swap(sol, cycle[0], cycle[1]); } print_sol({sol}); } void solve2(int n) { deque<int> sol1; deque<int> sol2; for (auto cycle : cycles) { /* for (int x : cycle) { cout << x << " "; } cout << " end of cycle\n"; */ if (cycle.size() == 1) continue; if (cycle.size() == 2) append_swap(sol1, cycle[0], cycle[1]); else { for (int i = 0; i < cycle.size()/2; i ++) { append_swap(sol1, cycle[i], cycle[cycle.size() - i - 1]); } for (int i = 1; i <= (cycle.size() - 1)/2; i ++) { append_swap(sol2, cycle[i], cycle[cycle.size() - i]); } } } print_sol({sol1, sol2}); } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; graph.resize(n+1); vis.resize(n+1); vector<pair<int,int>> v; for (int i = 0; i < n; i ++) { int x; cin >> x; v.emplace_back(x, i+1); } sort(v.begin(), v.end()); for (int i = 0; i < n; i ++) { graph[v[i].second] = i + 1; } for (int i = 1; i <= n; i ++) find_cycles(i); bool mozna0 = true; bool mozna1 = true; for (auto cycle : cycles) { if (cycle.size() > 2) mozna1 = false; if (cycle.size() > 1) mozna0 = false; } if (mozna0) print_sol({}); else if (mozna1) solve1(n); else solve2(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 | #ifndef LOCAL #pragma GCC optimize("O3") #endif #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define mor > muu & operator << ( #define R22(r) sim > typename \ enable_if<1 r sizeof dud<c>(0), muu&>::type operator<<(c g) { sim > struct rge {c b, e; }; sim > rge<c> range(c h, c n) { return {h, n}; } sim > auto dud(c * r) -> decltype(cerr << *r); sim > char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() { cerr << a.str() << endl; } R22(<) a << boolalpha << g; ris; } R22(==) ris << range(begin(g), end(g)); } sim, class b mor pair < b, c > r) { ris << "(" << r.first << ", " << r.second << ")"; } sim mor rge<c> u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } #else sim mor const c&) { ris; } #endif muu & operator()() { ris; } }; #define imie(r...) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ") vector<bool> vis; vector<int> graph; vector<vector<int>> cycles; void dfs(int x) { if (vis[x]) return; vis[x] = true; cycles.back().push_back(x); dfs(graph[x]); } void find_cycles(int x) { if (vis[x]) return; cycles.push_back({}); dfs(x); } void append_swap(deque<int>& sol, int a, int b) { sol.push_front(a); sol.push_back(b); } void print_sol(vector<deque<int>> v) { // assert(v.size() <= 2); cout << v.size() << "\n"; for (auto shuffle : v) { cout << shuffle.size() << "\n"; for (int x : shuffle) { cout << x << " "; } cout << "\n"; } } void solve1(int n) { deque<int> sol; for (auto cycle : cycles) { if (cycle.size() == 1) continue; append_swap(sol, cycle[0], cycle[1]); } print_sol({sol}); } void solve2(int n) { deque<int> sol1; deque<int> sol2; for (auto cycle : cycles) { /* for (int x : cycle) { cout << x << " "; } cout << " end of cycle\n"; */ if (cycle.size() == 1) continue; if (cycle.size() == 2) append_swap(sol1, cycle[0], cycle[1]); else { for (int i = 0; i < cycle.size()/2; i ++) { append_swap(sol1, cycle[i], cycle[cycle.size() - i - 1]); } for (int i = 1; i <= (cycle.size() - 1)/2; i ++) { append_swap(sol2, cycle[i], cycle[cycle.size() - i]); } } } print_sol({sol1, sol2}); } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; graph.resize(n+1); vis.resize(n+1); vector<pair<int,int>> v; for (int i = 0; i < n; i ++) { int x; cin >> x; v.emplace_back(x, i+1); } sort(v.begin(), v.end()); for (int i = 0; i < n; i ++) { graph[v[i].second] = i + 1; } for (int i = 1; i <= n; i ++) find_cycles(i); bool mozna0 = true; bool mozna1 = true; for (auto cycle : cycles) { if (cycle.size() > 2) mozna1 = false; if (cycle.size() > 1) mozna0 = false; } if (mozna0) print_sol({}); else if (mozna1) solve1(n); else solve2(n); } |