#include<bits/stdc++.h> #define poly vector<int> #define IOS ios::sync_with_stdio(false) #define ll long long #define mp make_pair #define mt make_tuple #define pa pair < int,int > #define fi first #define se second #define inf 1e9 #define mod 998244353 #define int ll #define N 3005 using namespace std; int n,p[N],vis[N]; int a[N],b[N]; vector<poly>ans; int pre(int x) { for (int i=1;i<=n;i++) { if (p[i]==x) return i; } return 0; } poly merge(poly x,poly y) { reverse(y.begin(),y.end()); for (auto u:y) x.push_back(u); return x; } void BellaKira() { cin>>n; for (int i=1;i<=n;i++) { cin>>a[i];b[i]=a[i]; } sort(b+1,b+n+1); for (int i=1;i<=n;i++) p[i]=lower_bound(b+1,b+n+1,a[i])-b; { poly X,Y; for (int i=1;i<=n;i++) { int x=i,y=p[i]; while (x!=y&&p[y]!=x) { int nx=pre(x),ny=p[y]; X.push_back(nx); Y.push_back(y); swap(p[nx],p[y]); x=nx,y=ny; } } if (X.size()) ans.push_back(merge(X,Y)); } { poly X,Y; for (int i=1;i<=n;i++) { if (i==p[i]) continue; X.push_back(i); Y.push_back(p[i]); swap(p[i],p[p[i]]); } if (X.size()) ans.push_back(merge(X,Y)); } cout<<ans.size()<<'\n'; for (auto u:ans) { cout<<u.size()<<"\n"; for (auto v:u) cout<<v<<" "; cout<<'\n'; } } signed main() { IOS; cin.tie(0); int T=1; while (T--) { BellaKira(); } }
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 | #include<bits/stdc++.h> #define poly vector<int> #define IOS ios::sync_with_stdio(false) #define ll long long #define mp make_pair #define mt make_tuple #define pa pair < int,int > #define fi first #define se second #define inf 1e9 #define mod 998244353 #define int ll #define N 3005 using namespace std; int n,p[N],vis[N]; int a[N],b[N]; vector<poly>ans; int pre(int x) { for (int i=1;i<=n;i++) { if (p[i]==x) return i; } return 0; } poly merge(poly x,poly y) { reverse(y.begin(),y.end()); for (auto u:y) x.push_back(u); return x; } void BellaKira() { cin>>n; for (int i=1;i<=n;i++) { cin>>a[i];b[i]=a[i]; } sort(b+1,b+n+1); for (int i=1;i<=n;i++) p[i]=lower_bound(b+1,b+n+1,a[i])-b; { poly X,Y; for (int i=1;i<=n;i++) { int x=i,y=p[i]; while (x!=y&&p[y]!=x) { int nx=pre(x),ny=p[y]; X.push_back(nx); Y.push_back(y); swap(p[nx],p[y]); x=nx,y=ny; } } if (X.size()) ans.push_back(merge(X,Y)); } { poly X,Y; for (int i=1;i<=n;i++) { if (i==p[i]) continue; X.push_back(i); Y.push_back(p[i]); swap(p[i],p[p[i]]); } if (X.size()) ans.push_back(merge(X,Y)); } cout<<ans.size()<<'\n'; for (auto u:ans) { cout<<u.size()<<"\n"; for (auto v:u) cout<<v<<" "; cout<<'\n'; } } signed main() { IOS; cin.tie(0); int T=1; while (T--) { BellaKira(); } } |