#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; const int MAXN = 3e3 + 5; int a[MAXN], dsc[MAXN]; int main(void) { int n; scanf("%d",&n); for(int i=1; i<=n; ++i) scanf("%d",&a[i]); for(int i=1; i<=n; ++i) dsc[i] = a[i]; sort(dsc+1, dsc+n+1); for(int i=1; i<=n; ++i) a[i] = lower_bound(dsc+1, dsc+n+1, a[i]) - dsc; vector< vector<int> > ans; while(1) { vector<int> A, B; static bool vis[MAXN]; memset(vis, 0, sizeof(vis)); for(int i=1; i<=n; ++i) if(!vis[i]) { int j = i; vector<int> pos; while(!vis[j]) pos.emplace_back(j), vis[j] = 1, j = a[j]; for(int x=0,y=(int)pos.size()-1; x<y; ++x, --y) { A.emplace_back(pos[x]); B.emplace_back(pos[y]); swap(a[pos[x]], a[pos[y]]); } } if(!A.size()) break; A.insert(A.end(), B.rbegin(), B.rend()); ans.emplace_back(A); } printf("%d\n",(int)ans.size()); for(auto vec: ans) { printf("%d\n",(int)vec.size()); for(auto t: vec) printf("%d ",t); printf("\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 59 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; const int MAXN = 3e3 + 5; int a[MAXN], dsc[MAXN]; int main(void) { int n; scanf("%d",&n); for(int i=1; i<=n; ++i) scanf("%d",&a[i]); for(int i=1; i<=n; ++i) dsc[i] = a[i]; sort(dsc+1, dsc+n+1); for(int i=1; i<=n; ++i) a[i] = lower_bound(dsc+1, dsc+n+1, a[i]) - dsc; vector< vector<int> > ans; while(1) { vector<int> A, B; static bool vis[MAXN]; memset(vis, 0, sizeof(vis)); for(int i=1; i<=n; ++i) if(!vis[i]) { int j = i; vector<int> pos; while(!vis[j]) pos.emplace_back(j), vis[j] = 1, j = a[j]; for(int x=0,y=(int)pos.size()-1; x<y; ++x, --y) { A.emplace_back(pos[x]); B.emplace_back(pos[y]); swap(a[pos[x]], a[pos[y]]); } } if(!A.size()) break; A.insert(A.end(), B.rbegin(), B.rend()); ans.emplace_back(A); } printf("%d\n",(int)ans.size()); for(auto vec: ans) { printf("%d\n",(int)vec.size()); for(auto t: vec) printf("%d ",t); printf("\n"); } return 0; } |