#include <bits/stdc++.h> using namespace std; pair <int,int> p1[3001]; int T[3001]; short odw[3001]; vector <int> q1; vector <int> qprz; vector <int> qdol; vector <int> q2prz; vector <int> q2dol; int main(){ int n; scanf("%d",&n); for (int i = 1; i <= n;i++){ scanf("%d",&p1[i].first); p1[i].second = i; } sort(p1+1,p1+n+1); for (int i = 1; i <= n;i++){ T[p1[i].second] = i; } int wynik = 0; for (int i = 1; i <= n;i++){ if (odw[i] == 0 && T[i] != i){ wynik = 1; int id = i; while (odw[id]==0){ odw[id] = 1; q1.push_back(id); id = T[id]; } for (int j = 0; j < q1.size()/2;j++){ qprz.push_back(q1[j]); swap(T[q1[j]],T[q1[q1.size()-1-j]]); } for (int j = q1.size()-1; j > (q1.size()-1)/2;j--){ qdol.push_back(q1[j]); } while (q1.size() > 0){ q1.pop_back(); } } else{ odw[i] = 1; } } // cout << wynik << endl; // for (int i = 1; i <= n;i++){ // cout << T[i] << " "; // } for (int i = 1; i <= n;i++){ if (odw[i] == 1 && T[i] != i){ wynik = 2; int id = i; while (odw[id]==1){ odw[id] = 2; q1.push_back(id); id = T[id]; } for (int j = 0; j < q1.size()/2;j++){ q2prz.push_back(q1[j]); //cout <<"s" <<T[q1[j]] << T[q1[q1.size()-1-j]] << endl; swap(T[q1[j]],T[q1[q1.size()-1-j]]); } for (int j = q1.size()-1; j > (q1.size()-1)/2;j--){ q2dol.push_back(q1[j]); } while (q1.size() > 0){ q1.pop_back(); } } } //cout << wynik << endl; //for (int i = 1; i <= n;i++){ //cout << T[i] << " "; //} if (wynik == 0){ printf("0"); } if (wynik == 1){ printf("1 \n"); printf("%d \n",qprz.size()+qdol.size()); for (int i =0; i < qprz.size();i++){ printf("%d ",qprz[i]); } for (int i =qdol.size() - 1; i >= 0;i--){ printf("%d ",qdol[i]); } } if (wynik == 2){ printf("2 \n"); printf("%d \n",qprz.size()+qdol.size()); for (int i =0; i < qprz.size();i++){ printf("%d ",qprz[i]); } for (int i =qprz.size() - 1; i >= 0;i--){ printf("%d ",qdol[i]); } printf("\n"); printf("%d \n",q2prz.size()+q2dol.size()); for (int i =0; i < q2prz.size();i++){ printf("%d ",q2prz[i]); } for (int i =q2dol.size() - 1; i >= 0;i--){ printf("%d ",q2dol[i]); } } }
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 102 103 104 105 106 107 108 109 110 111 | #include <bits/stdc++.h> using namespace std; pair <int,int> p1[3001]; int T[3001]; short odw[3001]; vector <int> q1; vector <int> qprz; vector <int> qdol; vector <int> q2prz; vector <int> q2dol; int main(){ int n; scanf("%d",&n); for (int i = 1; i <= n;i++){ scanf("%d",&p1[i].first); p1[i].second = i; } sort(p1+1,p1+n+1); for (int i = 1; i <= n;i++){ T[p1[i].second] = i; } int wynik = 0; for (int i = 1; i <= n;i++){ if (odw[i] == 0 && T[i] != i){ wynik = 1; int id = i; while (odw[id]==0){ odw[id] = 1; q1.push_back(id); id = T[id]; } for (int j = 0; j < q1.size()/2;j++){ qprz.push_back(q1[j]); swap(T[q1[j]],T[q1[q1.size()-1-j]]); } for (int j = q1.size()-1; j > (q1.size()-1)/2;j--){ qdol.push_back(q1[j]); } while (q1.size() > 0){ q1.pop_back(); } } else{ odw[i] = 1; } } // cout << wynik << endl; // for (int i = 1; i <= n;i++){ // cout << T[i] << " "; // } for (int i = 1; i <= n;i++){ if (odw[i] == 1 && T[i] != i){ wynik = 2; int id = i; while (odw[id]==1){ odw[id] = 2; q1.push_back(id); id = T[id]; } for (int j = 0; j < q1.size()/2;j++){ q2prz.push_back(q1[j]); //cout <<"s" <<T[q1[j]] << T[q1[q1.size()-1-j]] << endl; swap(T[q1[j]],T[q1[q1.size()-1-j]]); } for (int j = q1.size()-1; j > (q1.size()-1)/2;j--){ q2dol.push_back(q1[j]); } while (q1.size() > 0){ q1.pop_back(); } } } //cout << wynik << endl; //for (int i = 1; i <= n;i++){ //cout << T[i] << " "; //} if (wynik == 0){ printf("0"); } if (wynik == 1){ printf("1 \n"); printf("%d \n",qprz.size()+qdol.size()); for (int i =0; i < qprz.size();i++){ printf("%d ",qprz[i]); } for (int i =qdol.size() - 1; i >= 0;i--){ printf("%d ",qdol[i]); } } if (wynik == 2){ printf("2 \n"); printf("%d \n",qprz.size()+qdol.size()); for (int i =0; i < qprz.size();i++){ printf("%d ",qprz[i]); } for (int i =qprz.size() - 1; i >= 0;i--){ printf("%d ",qdol[i]); } printf("\n"); printf("%d \n",q2prz.size()+q2dol.size()); for (int i =0; i < q2prz.size();i++){ printf("%d ",q2prz[i]); } for (int i =q2dol.size() - 1; i >= 0;i--){ printf("%d ",q2dol[i]); } } } |