#include<bits/stdc++.h> #define ll long long using namespace std; const int N=3e3+5; pair<int,int>a[N]; bool vis[N]; int n,p[N]; signed main() { ios::sync_with_stdio(false),cin.tie(0); cout.precision(10),cout.setf(ios::fixed); cin>>n; for (int i=1;i<=n;i++) { cin>>a[i].first; a[i].second=i; } sort(a+1,a+n+1); for (int i=1;i<=n;i++) { p[a[i].second]=i; } vector<vector<int>>ans; while (1) { vector<int>A,B; memset(vis,0,sizeof(vis)); for (int i=1;i<=n;i++) { if (!vis[i]) { vector<int>b; for (int j=i;!vis[j];j=p[j]) { b.emplace_back(j); vis[j]=1; } for (int j=0,k=(int)b.size()-1;j<k;j++,k--) { A.emplace_back(b[j]); B.emplace_back(b[k]); swap(p[b[j]],p[b[k]]); } } } reverse(A.begin(),A.end()); A.insert(A.end(),B.begin(),B.end()); if (A.empty()) { break; } ans.emplace_back(A); } cout<<ans.size()<<"\n"; for (auto i:ans) { cout<<i.size()<<"\n"; for (int j:i) { cout<<j<<' '; } cout<<"\n"; } 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 | #include<bits/stdc++.h> #define ll long long using namespace std; const int N=3e3+5; pair<int,int>a[N]; bool vis[N]; int n,p[N]; signed main() { ios::sync_with_stdio(false),cin.tie(0); cout.precision(10),cout.setf(ios::fixed); cin>>n; for (int i=1;i<=n;i++) { cin>>a[i].first; a[i].second=i; } sort(a+1,a+n+1); for (int i=1;i<=n;i++) { p[a[i].second]=i; } vector<vector<int>>ans; while (1) { vector<int>A,B; memset(vis,0,sizeof(vis)); for (int i=1;i<=n;i++) { if (!vis[i]) { vector<int>b; for (int j=i;!vis[j];j=p[j]) { b.emplace_back(j); vis[j]=1; } for (int j=0,k=(int)b.size()-1;j<k;j++,k--) { A.emplace_back(b[j]); B.emplace_back(b[k]); swap(p[b[j]],p[b[k]]); } } } reverse(A.begin(),A.end()); A.insert(A.end(),B.begin(),B.end()); if (A.empty()) { break; } ans.emplace_back(A); } cout<<ans.size()<<"\n"; for (auto i:ans) { cout<<i.size()<<"\n"; for (int j:i) { cout<<j<<' '; } cout<<"\n"; } return 0; } |