#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int N; cin>>N; int h[3000]; for(int i=0; i<N; ++i) cin>>h[i]; vector< pair<int,int> > V; for(int i=0; i<N; ++i) { V.push_back(make_pair(h[i],i)); } sort(V.begin(),V.end()); vector< vector<int> > P; bool cz[3000],czy1; czy1 = true; for(int i=0; i<N; ++i) cz[i] = true; for(int i=0; i<N; ++i) { vector<int> C; if(cz[i]) { int M; M = V[i].second; C.push_back(i); while(M != i) { C.push_back(M); cz[M] = false; M = V[M].second; } cz[i] = false; if(C.size() > 1) P.push_back(C); if(C.size() > 2) czy1 = false; } } vector<int> L1,L2,P1,P2; for(int i=0; i<P.size(); ++i) { int R = P[i].size(); for(int j=0; j<R/2; ++j) { L1.push_back(P[i][j]); P1.push_back(P[i][R-1-j]); } for(int j=1; j<(R+1)/2; ++j) { L2.push_back(P[i][j]); P2.push_back(P[i][R-j]); } } if(P.size() == 0) { cout<<0; } else if(czy1) { cout<<1<<endl; cout<<L1.size()+P1.size()<<endl; for(int i=0; i<L1.size(); ++i) cout<<L1[i]+1<<" "; for(int i=0; i<P1.size(); ++i) cout<<P1[P1.size()-1-i]+1<<" "; } else { cout<<2<<endl; cout<<L2.size()+P2.size()<<endl; for(int i=0; i<L2.size(); ++i) cout<<L2[i]+1<<" "; for(int i=0; i<P2.size(); ++i) cout<<P2[P2.size()-1-i]+1<<" "; cout<<endl; cout<<L1.size()+P1.size()<<endl; for(int i=0; i<L1.size(); ++i) cout<<L1[i]+1<<" "; for(int i=0; i<P1.size(); ++i) cout<<P1[P1.size()-1-i]+1<<" "; } 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int N; cin>>N; int h[3000]; for(int i=0; i<N; ++i) cin>>h[i]; vector< pair<int,int> > V; for(int i=0; i<N; ++i) { V.push_back(make_pair(h[i],i)); } sort(V.begin(),V.end()); vector< vector<int> > P; bool cz[3000],czy1; czy1 = true; for(int i=0; i<N; ++i) cz[i] = true; for(int i=0; i<N; ++i) { vector<int> C; if(cz[i]) { int M; M = V[i].second; C.push_back(i); while(M != i) { C.push_back(M); cz[M] = false; M = V[M].second; } cz[i] = false; if(C.size() > 1) P.push_back(C); if(C.size() > 2) czy1 = false; } } vector<int> L1,L2,P1,P2; for(int i=0; i<P.size(); ++i) { int R = P[i].size(); for(int j=0; j<R/2; ++j) { L1.push_back(P[i][j]); P1.push_back(P[i][R-1-j]); } for(int j=1; j<(R+1)/2; ++j) { L2.push_back(P[i][j]); P2.push_back(P[i][R-j]); } } if(P.size() == 0) { cout<<0; } else if(czy1) { cout<<1<<endl; cout<<L1.size()+P1.size()<<endl; for(int i=0; i<L1.size(); ++i) cout<<L1[i]+1<<" "; for(int i=0; i<P1.size(); ++i) cout<<P1[P1.size()-1-i]+1<<" "; } else { cout<<2<<endl; cout<<L2.size()+P2.size()<<endl; for(int i=0; i<L2.size(); ++i) cout<<L2[i]+1<<" "; for(int i=0; i<P2.size(); ++i) cout<<P2[P2.size()-1-i]+1<<" "; cout<<endl; cout<<L1.size()+P1.size()<<endl; for(int i=0; i<L1.size(); ++i) cout<<L1[i]+1<<" "; for(int i=0; i<P1.size(); ++i) cout<<P1[P1.size()-1-i]+1<<" "; } cout<<endl; return 0; } |