#define pb push_back #define mp make_pair #define fi first #define se second #define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__) #define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();} #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector <int> vi; typedef pair<int,int> PII; typedef pair<ll,ll> PLL; constexpr ll nax = 2e5+6969, INF = 2e9+6969; int n, t[nax], perm[nax]; vector<vector<PII>> ans; map<int, int> order; bitset <nax> vis; void find_cycle(int pos, vi & cycle) { cycle.pb(pos); vis[pos] = 1; if(perm[pos] != cycle[0]) { find_cycle(perm[pos], cycle); } } void print_answer() { printf("%d\n", (int)ans.size()); for(auto v: ans) { printf("%d\n", (int)v.size() * 2); for(int i=0;i<v.size();i++) { printf("%d ", v[i].fi); } for(int i=v.size() - 1; i >= 0; i--) { printf("%d ", v[i].se); } puts(""); } puts(""); } void solve(vi cycle, int depth) { while(ans.size() <= depth) { ans.pb(vector<PII>()); } if(cycle.size() == 2) { ans[depth].pb(mp(cycle[0], cycle[1])); return; } int N = cycle.size(); for(int i=0;i<cycle.size()/2;i++) { ans[depth].pb(mp(cycle[i], cycle[N - i - 1])); } } void doSwap(int depth) { if(ans.size() == 0) { return; } if(ans.size() <= depth) return; for(auto e: ans[depth]) { int x = e.fi; int y = e.se; int posx = t[x]; int posy = t[y]; swap(perm[posx], perm[posy]); t[x] = posy; t[y] = posx; } } int main() { scanf("%d", &n); for(int i=1;i<=n;i++) { scanf("%d", &t[i]); order[t[i]] = 0; } int cnt = 1; for(auto &e: order) { e.se = cnt; cnt++; } for(int i=1;i<=n;i++) { t[i] = order[t[i]]; perm[t[i]] = i; } // for(int i=1;i<=n;i++) { // printf("%d ", t[i]); // } for(int turn = 0;; turn++) { bool stop = true; for(int i=1;i<=n;i++) { if(vis[i]) continue; vi cycle; find_cycle(i, cycle); // cout << "["; // for(auto e: cycle) { // cout << e << " "; // } // cout << "]\n"; if(cycle.size() > 1) { stop = false; solve(cycle, turn); } } if(stop) { break; } doSwap(turn); for(int i=1;i<=n;i++) vis[i] = 0; } print_answer(); 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 | #define pb push_back #define mp make_pair #define fi first #define se second #define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__) #define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();} #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector <int> vi; typedef pair<int,int> PII; typedef pair<ll,ll> PLL; constexpr ll nax = 2e5+6969, INF = 2e9+6969; int n, t[nax], perm[nax]; vector<vector<PII>> ans; map<int, int> order; bitset <nax> vis; void find_cycle(int pos, vi & cycle) { cycle.pb(pos); vis[pos] = 1; if(perm[pos] != cycle[0]) { find_cycle(perm[pos], cycle); } } void print_answer() { printf("%d\n", (int)ans.size()); for(auto v: ans) { printf("%d\n", (int)v.size() * 2); for(int i=0;i<v.size();i++) { printf("%d ", v[i].fi); } for(int i=v.size() - 1; i >= 0; i--) { printf("%d ", v[i].se); } puts(""); } puts(""); } void solve(vi cycle, int depth) { while(ans.size() <= depth) { ans.pb(vector<PII>()); } if(cycle.size() == 2) { ans[depth].pb(mp(cycle[0], cycle[1])); return; } int N = cycle.size(); for(int i=0;i<cycle.size()/2;i++) { ans[depth].pb(mp(cycle[i], cycle[N - i - 1])); } } void doSwap(int depth) { if(ans.size() == 0) { return; } if(ans.size() <= depth) return; for(auto e: ans[depth]) { int x = e.fi; int y = e.se; int posx = t[x]; int posy = t[y]; swap(perm[posx], perm[posy]); t[x] = posy; t[y] = posx; } } int main() { scanf("%d", &n); for(int i=1;i<=n;i++) { scanf("%d", &t[i]); order[t[i]] = 0; } int cnt = 1; for(auto &e: order) { e.se = cnt; cnt++; } for(int i=1;i<=n;i++) { t[i] = order[t[i]]; perm[t[i]] = i; } // for(int i=1;i<=n;i++) { // printf("%d ", t[i]); // } for(int turn = 0;; turn++) { bool stop = true; for(int i=1;i<=n;i++) { if(vis[i]) continue; vi cycle; find_cycle(i, cycle); // cout << "["; // for(auto e: cycle) { // cout << e << " "; // } // cout << "]\n"; if(cycle.size() > 1) { stop = false; solve(cycle, turn); } } if(stop) { break; } doSwap(turn); for(int i=1;i<=n;i++) vis[i] = 0; } print_answer(); return 0; } |