#include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; int arr[3005]; int vis[3005]; vector<int> resb[2]; vector<int> rese[2]; int res = 0; void solve(vector<int> cycle){ if(cycle.size() == 1) return; if(cycle.size() == 2){ resb[0].push_back(cycle[0]); rese[0].push_back(cycle[1]); res = max(res, 1); } else{ res = max(res, 2); for(int l=0, r=cycle.size()-1; l<r; l++, r--){ resb[0].push_back(cycle[l]); rese[0].push_back(cycle[r]); } for(int l=1, r=cycle.size()-1; l<r; l++, r--){ resb[1].push_back(cycle[l]); rese[1].push_back(cycle[r]); } } } int main(){ ios_base::sync_with_stdio(0); int n; cin>>n; vector<PII> P; for(int i=1;i<=n;i++){ int a; cin>>a; P.push_back({a,i}); } sort(P.begin(), P.end()); for(int i=0;i<n;i++) arr[P[i].second] = i+1; for(int i=1;i<=n;i++){ if(vis[i]) continue; vector<int> cycle; vis[i] = 1; int v = i; cycle.push_back(v); v = arr[v]; while(!vis[v]){ cycle.push_back(v); vis[v] = 1; v = arr[v]; } solve(cycle); } cout<<res<<endl; for(int i=0;i<res;i++){ cout<<resb[i].size()+rese[i].size()<<endl; reverse(rese[i].begin(), rese[i].end()); for(int u: resb[i]) cout<<u<<" "; for(int u: rese[i]) cout<<u<<" "; cout<<endl; } }
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 | #include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; int arr[3005]; int vis[3005]; vector<int> resb[2]; vector<int> rese[2]; int res = 0; void solve(vector<int> cycle){ if(cycle.size() == 1) return; if(cycle.size() == 2){ resb[0].push_back(cycle[0]); rese[0].push_back(cycle[1]); res = max(res, 1); } else{ res = max(res, 2); for(int l=0, r=cycle.size()-1; l<r; l++, r--){ resb[0].push_back(cycle[l]); rese[0].push_back(cycle[r]); } for(int l=1, r=cycle.size()-1; l<r; l++, r--){ resb[1].push_back(cycle[l]); rese[1].push_back(cycle[r]); } } } int main(){ ios_base::sync_with_stdio(0); int n; cin>>n; vector<PII> P; for(int i=1;i<=n;i++){ int a; cin>>a; P.push_back({a,i}); } sort(P.begin(), P.end()); for(int i=0;i<n;i++) arr[P[i].second] = i+1; for(int i=1;i<=n;i++){ if(vis[i]) continue; vector<int> cycle; vis[i] = 1; int v = i; cycle.push_back(v); v = arr[v]; while(!vis[v]){ cycle.push_back(v); vis[v] = 1; v = arr[v]; } solve(cycle); } cout<<res<<endl; for(int i=0;i<res;i++){ cout<<resb[i].size()+rese[i].size()<<endl; reverse(rese[i].begin(), rese[i].end()); for(int u: resb[i]) cout<<u<<" "; for(int u: rese[i]) cout<<u<<" "; cout<<endl; } } |