#include<iostream> #include<vector> #include<algorithm> #include<utility> using namespace std; void dfs(int i, vector<int> &A, vector<int> &B,vector<int> &C,vector<int> &N,vector<int> &L1,vector<int> &L2,vector<int> &P1,vector<int> &P2,vector<bool> &V){ //cout << "node: " << i <<'\n'; if(V[i])return; V[i] = true; if(B[i] == N[i]){ //cout << "A\n"; L2.push_back(C[N[i]]); P2.push_back(C[i]); V[N[i]]=true; return; } if(N[N[i]] == B[i]){ //cout << "B\n"; L1.push_back(C[B[i]]);P1.push_back(C[i]); L2.push_back(C[B[i]]);P2.push_back(C[N[i]]); V[N[i]] = true; V[B[i]] = true; return; } //cout << "C\n"; V[N[i]]=true; L1.push_back(C[N[N[i]]]);P1.push_back(C[i]); L2.push_back(C[N[N[i]]]);P2.push_back(C[N[i]]); C[N[N[i]]] = C[i]; B[N[N[i]]]=B[i]; N[B[i]] = N[N[i]]; dfs(B[i],A,B,C,N,L1,L2,P1,P2,V); } int main(){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0); int n; cin >> n; vector<pair<int,int> > T(n); for(int i=0;i<n;i++){ cin >> T[i].first; T[i].second = i; } sort(T.begin(),T.end()); vector<int> A(n),C(n); for(int i=0;i<n;i++)A[T[i].second] = i; T.resize(0); for(int i=0;i<n;i++)C[A[i]] = i+1; // cout << "A: "; // for(int i=0;i<n;i++) cout << A[i] << ' '; // cout << '\n'; vector<int> B(n, -1), N(n, -1); for(int i=0;i<n;i++){ if(i!=A[i]){ N[A[i]] = A[A[i]]; B[A[A[i]]] = A[i]; } } //cout << "B N\n"; //for(int i=0;i<n;i++) cout << B[i] << ' ' << N[i] << '\n'; vector<int> L1,L2,P1,P2; vector<bool> V(n,false); for(int i=0;i<n;i++){ if(A[i]!=i)dfs(A[i],A,B,C,N,L1,L2,P1,P2,V); } reverse(P1.begin(),P1.end()); reverse(P2.begin(),P2.end()); L1.insert(L1.end(),P1.begin(),P1.end()); L2.insert(L2.end(),P2.begin(),P2.end()); if(L1.size() == 0 && L2.size() == 0)cout << 0 << '\n'; else if(L1.size()==0){ cout << 1 << '\n'; cout << L2.size() << '\n'; for(int i=0;i<L2.size();i++) cout << L2[i] << ' '; cout << '\n'; } else{ cout << 2 << '\n'; cout << L1.size() << '\n'; for(int i=0;i<L1.size();i++) cout << L1[i] << ' '; cout << '\n'; cout << L2.size() << '\n'; for(int i=0;i<L2.size();i++) cout << L2[i] << ' '; cout << '\n'; } }
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 | #include<iostream> #include<vector> #include<algorithm> #include<utility> using namespace std; void dfs(int i, vector<int> &A, vector<int> &B,vector<int> &C,vector<int> &N,vector<int> &L1,vector<int> &L2,vector<int> &P1,vector<int> &P2,vector<bool> &V){ //cout << "node: " << i <<'\n'; if(V[i])return; V[i] = true; if(B[i] == N[i]){ //cout << "A\n"; L2.push_back(C[N[i]]); P2.push_back(C[i]); V[N[i]]=true; return; } if(N[N[i]] == B[i]){ //cout << "B\n"; L1.push_back(C[B[i]]);P1.push_back(C[i]); L2.push_back(C[B[i]]);P2.push_back(C[N[i]]); V[N[i]] = true; V[B[i]] = true; return; } //cout << "C\n"; V[N[i]]=true; L1.push_back(C[N[N[i]]]);P1.push_back(C[i]); L2.push_back(C[N[N[i]]]);P2.push_back(C[N[i]]); C[N[N[i]]] = C[i]; B[N[N[i]]]=B[i]; N[B[i]] = N[N[i]]; dfs(B[i],A,B,C,N,L1,L2,P1,P2,V); } int main(){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0); int n; cin >> n; vector<pair<int,int> > T(n); for(int i=0;i<n;i++){ cin >> T[i].first; T[i].second = i; } sort(T.begin(),T.end()); vector<int> A(n),C(n); for(int i=0;i<n;i++)A[T[i].second] = i; T.resize(0); for(int i=0;i<n;i++)C[A[i]] = i+1; // cout << "A: "; // for(int i=0;i<n;i++) cout << A[i] << ' '; // cout << '\n'; vector<int> B(n, -1), N(n, -1); for(int i=0;i<n;i++){ if(i!=A[i]){ N[A[i]] = A[A[i]]; B[A[A[i]]] = A[i]; } } //cout << "B N\n"; //for(int i=0;i<n;i++) cout << B[i] << ' ' << N[i] << '\n'; vector<int> L1,L2,P1,P2; vector<bool> V(n,false); for(int i=0;i<n;i++){ if(A[i]!=i)dfs(A[i],A,B,C,N,L1,L2,P1,P2,V); } reverse(P1.begin(),P1.end()); reverse(P2.begin(),P2.end()); L1.insert(L1.end(),P1.begin(),P1.end()); L2.insert(L2.end(),P2.begin(),P2.end()); if(L1.size() == 0 && L2.size() == 0)cout << 0 << '\n'; else if(L1.size()==0){ cout << 1 << '\n'; cout << L2.size() << '\n'; for(int i=0;i<L2.size();i++) cout << L2[i] << ' '; cout << '\n'; } else{ cout << 2 << '\n'; cout << L1.size() << '\n'; for(int i=0;i<L1.size();i++) cout << L1[i] << ' '; cout << '\n'; cout << L2.size() << '\n'; for(int i=0;i<L2.size();i++) cout << L2[i] << ' '; cout << '\n'; } } |