#include<bits/stdc++.h> #define pb push_back #define fi first #define se second #define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define vll vector<pll> #define vii vector<pii> #define vi vector<int> #define vl vector<ll> #define vvi vector<vi> #define vvii vector<vii> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vb> #define mid (l+r)/2 #define endl '\n' #define lowbit(x) x&(-x) #define bits(x) __builtin_popcount(x) #define mins(se) (*se.begin()) #define maxs(se) (*--se.end()) #define ALL(x) x.begin(), x.end() using namespace std; vvii res; vi v; vb vis; void cyklTescia(int e, vi& sto) { if(vis[e]) return; vis[e] = true; sto.pb(e); cyklTescia(v[e], sto); } int main() { turbo int n; cin >> n; vii prz(n); v = vi(n); for(int i = 0; i < n; i++) { cin >> prz[i].fi; prz[i].se = i; } sort(ALL(prz)); for(int i = 0; i < n; i++) v[prz[i].se] = i; while(true) { vis = vb(n); vvi cykle; vii pom; for(int i = 0; i < n; i++) if(!vis[i] and v[i] != i) { vi sto; cyklTescia(i, sto); cykle.pb(move(sto)); } for(auto &ck : cykle) { if(ck.size() == 2) { pom.pb({ck[0]+1, ck[1]+1}); swap(v[ck[0]], v[ck[1]]); continue; } int i = 1, j = ck.size()-1; while(i < j) { pom.pb({ck[i]+1, ck[j]+1}); swap(v[ck[i]], v[ck[j]]); i++; j--; } } if(pom.empty()) break; res.pb(move(pom)); } cout << res.size() << "\n"; for(auto &ve : res) { cout << ve.size()*2 << "\n"; for(auto &it : ve) cout << it.fi << " "; for(int i = ve.size()-1; i >= 0; i--) cout << ve[i].se << " "; 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 | #include<bits/stdc++.h> #define pb push_back #define fi first #define se second #define turbo ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define vll vector<pll> #define vii vector<pii> #define vi vector<int> #define vl vector<ll> #define vvi vector<vi> #define vvii vector<vii> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vb> #define mid (l+r)/2 #define endl '\n' #define lowbit(x) x&(-x) #define bits(x) __builtin_popcount(x) #define mins(se) (*se.begin()) #define maxs(se) (*--se.end()) #define ALL(x) x.begin(), x.end() using namespace std; vvii res; vi v; vb vis; void cyklTescia(int e, vi& sto) { if(vis[e]) return; vis[e] = true; sto.pb(e); cyklTescia(v[e], sto); } int main() { turbo int n; cin >> n; vii prz(n); v = vi(n); for(int i = 0; i < n; i++) { cin >> prz[i].fi; prz[i].se = i; } sort(ALL(prz)); for(int i = 0; i < n; i++) v[prz[i].se] = i; while(true) { vis = vb(n); vvi cykle; vii pom; for(int i = 0; i < n; i++) if(!vis[i] and v[i] != i) { vi sto; cyklTescia(i, sto); cykle.pb(move(sto)); } for(auto &ck : cykle) { if(ck.size() == 2) { pom.pb({ck[0]+1, ck[1]+1}); swap(v[ck[0]], v[ck[1]]); continue; } int i = 1, j = ck.size()-1; while(i < j) { pom.pb({ck[i]+1, ck[j]+1}); swap(v[ck[i]], v[ck[j]]); i++; j--; } } if(pom.empty()) break; res.pb(move(pom)); } cout << res.size() << "\n"; for(auto &ve : res) { cout << ve.size()*2 << "\n"; for(auto &it : ve) cout << it.fi << " "; for(int i = ve.size()-1; i >= 0; i--) cout << ve[i].se << " "; cout << "\n"; } } |