#include<bits/stdc++.h> using namespace std; int tab[3009]; bool zaj[3009]; deque<int>A,B; int main() {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int n; cin>>n; vector<pair<int,int>>skal; for(int i=1;i<=n;i++) { cin>>tab[i]; skal.push_back({tab[i],i}); } sort(skal.begin(),skal.end()); int h=0; for(int i=0;i<skal.size();i++) { if(i==0||skal[i-1].first!=skal[i].first) { h++; } tab[skal[i].second]=h; } for(int i=1;i<=n;i++) { if(zaj[i]==1){continue;} vector<int>U; int v=i; while(zaj[v]==0) { U.push_back(v); zaj[v]=1; v=tab[v]; } if(U.size()==2) { A.push_front(U[0]); A.push_back(U[1]); } else if(U.size()>=3) { int uwu=U.size()/2; if(U.size()%2==0) { for(int i=0;i<uwu;i++) { A.push_front(U[i]); A.push_back(U[U.size()-i-1]); } for(int i=1;i<uwu;i++) { B.push_front(U[i]); B.push_back(U[U.size()-i]); } } else { for(int i=0;i<uwu;i++) { A.push_front(U[i]); A.push_back(U[U.size()-i-1]); } for(int i=1;i<=uwu;i++) { B.push_front(U[i]); B.push_back(U[U.size()-i]); } } } } if(A.size()==0){cout<<0<<endl;} else if(B.size()==0) { cout<<1<<endl; cout<<A.size()<<endl; for(int i=0;i<A.size();i++) { cout<<A[i]<<" "; } cout<<endl; } else { cout<<2<<endl; cout<<A.size()<<endl; for(int i=0;i<A.size();i++) { cout<<A[i]<<" "; } cout<<endl; cout<<B.size()<<endl; for(int i=0;i<B.size();i++) { cout<<B[i]<<" "; } cout<<endl; } 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 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 | #include<bits/stdc++.h> using namespace std; int tab[3009]; bool zaj[3009]; deque<int>A,B; int main() {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int n; cin>>n; vector<pair<int,int>>skal; for(int i=1;i<=n;i++) { cin>>tab[i]; skal.push_back({tab[i],i}); } sort(skal.begin(),skal.end()); int h=0; for(int i=0;i<skal.size();i++) { if(i==0||skal[i-1].first!=skal[i].first) { h++; } tab[skal[i].second]=h; } for(int i=1;i<=n;i++) { if(zaj[i]==1){continue;} vector<int>U; int v=i; while(zaj[v]==0) { U.push_back(v); zaj[v]=1; v=tab[v]; } if(U.size()==2) { A.push_front(U[0]); A.push_back(U[1]); } else if(U.size()>=3) { int uwu=U.size()/2; if(U.size()%2==0) { for(int i=0;i<uwu;i++) { A.push_front(U[i]); A.push_back(U[U.size()-i-1]); } for(int i=1;i<uwu;i++) { B.push_front(U[i]); B.push_back(U[U.size()-i]); } } else { for(int i=0;i<uwu;i++) { A.push_front(U[i]); A.push_back(U[U.size()-i-1]); } for(int i=1;i<=uwu;i++) { B.push_front(U[i]); B.push_back(U[U.size()-i]); } } } } if(A.size()==0){cout<<0<<endl;} else if(B.size()==0) { cout<<1<<endl; cout<<A.size()<<endl; for(int i=0;i<A.size();i++) { cout<<A[i]<<" "; } cout<<endl; } else { cout<<2<<endl; cout<<A.size()<<endl; for(int i=0;i<A.size();i++) { cout<<A[i]<<" "; } cout<<endl; cout<<B.size()<<endl; for(int i=0;i<B.size();i++) { cout<<B[i]<<" "; } cout<<endl; } return 0; } |