#include<bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("trapv") #define st first #define nd second #define pb push_back #define pp pop_back #define eb emplace_back #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; ///~~~~~~~~~~~~~~~~~~~~~~~~~~ template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.st<<", "<<m.nd<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void debug(){cerr<<"\n";} template <typename H, typename... T> void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);} #define deb(x...) cerr<<#x<<" = ";debug(x); ///~~~~~~~~~~~~~~~~~~~~~~~~~ typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N=2e5+5, INF=1e9+5, mod=1e9+7; int main(){ //BOOST; vector<vi> ans; int n; cin>>n; vii V(n); for(int i=0; i<n; i++)cin>>V[i].st, V[i].nd=i; sor(V); for(int i=0; i<n; i++)V[i].st=i, swap(V[i].st, V[i].nd); sor(V); while(true){ vi V2, V3; vi vis(n); for(int i=0; i<n; i++){ if(!vis[i]){ vector<int> VV; VV.pb(i); while(V[VV.back()].nd!=i){ VV.pb(V[VV.back()].nd); } for(int i:VV)vis[i]=1; for(int i=0; i<VV.size()/2; i++){ V2.pb(VV[i]); V3.pb(VV[VV.size()-i-1]); } } } if(!V2.size())break; for(int i=0; i<V2.size(); i++){ swap(V[V2[i]].nd, V[V3[i]].nd); } //deb(V2, V3); ans.pb({}); for(int i:V2)ans.back().pb(i); rev(V3); for(int i:V3)ans.back().pb(i); } cout<<ans.size()<<"\n"; for (auto VV:ans){ cout<<VV.size()<<"\n"; for(int i:VV)cout<<i+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 | #include<bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("trapv") #define st first #define nd second #define pb push_back #define pp pop_back #define eb emplace_back #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; ///~~~~~~~~~~~~~~~~~~~~~~~~~~ template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.st<<", "<<m.nd<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void debug(){cerr<<"\n";} template <typename H, typename... T> void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);} #define deb(x...) cerr<<#x<<" = ";debug(x); ///~~~~~~~~~~~~~~~~~~~~~~~~~ typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N=2e5+5, INF=1e9+5, mod=1e9+7; int main(){ //BOOST; vector<vi> ans; int n; cin>>n; vii V(n); for(int i=0; i<n; i++)cin>>V[i].st, V[i].nd=i; sor(V); for(int i=0; i<n; i++)V[i].st=i, swap(V[i].st, V[i].nd); sor(V); while(true){ vi V2, V3; vi vis(n); for(int i=0; i<n; i++){ if(!vis[i]){ vector<int> VV; VV.pb(i); while(V[VV.back()].nd!=i){ VV.pb(V[VV.back()].nd); } for(int i:VV)vis[i]=1; for(int i=0; i<VV.size()/2; i++){ V2.pb(VV[i]); V3.pb(VV[VV.size()-i-1]); } } } if(!V2.size())break; for(int i=0; i<V2.size(); i++){ swap(V[V2[i]].nd, V[V3[i]].nd); } //deb(V2, V3); ans.pb({}); for(int i:V2)ans.back().pb(i); rev(V3); for(int i:V3)ans.back().pb(i); } cout<<ans.size()<<"\n"; for (auto VV:ans){ cout<<VV.size()<<"\n"; for(int i:VV)cout<<i+1<<" "; cout<<"\n"; } } |