#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int i; int n; cin>>n; pair<int,int> tab[n+1]; for (i = 1; i < n+1; i++){ cin>>tab[i].first; tab[i].second = i; } stable_sort(tab+1,tab+n+1); int perm[n+1]; for (i = 1; i < n+1; i++){ perm[tab[i].second] = i; } bool czyzero = 1; bool czyjeden = 1; for (i = 1; i < n+1; i++){ if (perm[i] != i) czyzero = 0; if (perm[perm[i]] != i) czyjeden = 0; } if (czyzero){ cout<<0<<"\n"; return 0; } if (czyjeden){ cout<<1<<"\n"; vector<int> wy1; vector<int> wy2; for (i = 1; i < n+1; i++){ if (perm[i] > i){ wy1.push_back(i); wy2.push_back(perm[i]); } } reverse(wy2.begin(),wy2.end()); for (int j : wy2) wy1.push_back(j); cout<<wy1.size()<<"\n"; for (int j : wy1) cout<<j<<" "; cout<<"\n"; return 0; } cout<<2<<"\n"; bool vis[n+1]; for (i = 1; i < n+1; i++) vis[i] = 0; vector<int> wy1; vector<int> wy2; for (i = 1; i < n+1; i++){ if (vis[i]) continue; int x = perm[i]; vector<int> akt; while (x != i){ vis[x] = 1; akt.push_back(x); x = perm[x]; } for (int j = 0; j < (akt.size())/2; j++){ wy1.push_back(akt[j]); wy2.push_back(akt[akt.size()-1-j]); } } for (i = 0; i < (int)wy1.size(); i++) swap(perm[wy1[i]],perm[wy2[i]]); reverse(wy2.begin(),wy2.end()); cout<<wy1.size()+wy2.size()<<"\n"; for (int j : wy1) cout<<j<<" "; for (int j : wy2) cout<<j<<" "; cout<<"\n"; wy1.clear(); wy2.clear(); for (i = 1; i < n+1; i++){ if (perm[i] > i){ wy1.push_back(i); wy2.push_back(perm[i]); } } reverse(wy2.begin(),wy2.end()); for (int j : wy2) wy1.push_back(j); cout<<wy1.size()<<"\n"; for (int j : wy1) cout<<j<<" "; cout<<"\n"; 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 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int i; int n; cin>>n; pair<int,int> tab[n+1]; for (i = 1; i < n+1; i++){ cin>>tab[i].first; tab[i].second = i; } stable_sort(tab+1,tab+n+1); int perm[n+1]; for (i = 1; i < n+1; i++){ perm[tab[i].second] = i; } bool czyzero = 1; bool czyjeden = 1; for (i = 1; i < n+1; i++){ if (perm[i] != i) czyzero = 0; if (perm[perm[i]] != i) czyjeden = 0; } if (czyzero){ cout<<0<<"\n"; return 0; } if (czyjeden){ cout<<1<<"\n"; vector<int> wy1; vector<int> wy2; for (i = 1; i < n+1; i++){ if (perm[i] > i){ wy1.push_back(i); wy2.push_back(perm[i]); } } reverse(wy2.begin(),wy2.end()); for (int j : wy2) wy1.push_back(j); cout<<wy1.size()<<"\n"; for (int j : wy1) cout<<j<<" "; cout<<"\n"; return 0; } cout<<2<<"\n"; bool vis[n+1]; for (i = 1; i < n+1; i++) vis[i] = 0; vector<int> wy1; vector<int> wy2; for (i = 1; i < n+1; i++){ if (vis[i]) continue; int x = perm[i]; vector<int> akt; while (x != i){ vis[x] = 1; akt.push_back(x); x = perm[x]; } for (int j = 0; j < (akt.size())/2; j++){ wy1.push_back(akt[j]); wy2.push_back(akt[akt.size()-1-j]); } } for (i = 0; i < (int)wy1.size(); i++) swap(perm[wy1[i]],perm[wy2[i]]); reverse(wy2.begin(),wy2.end()); cout<<wy1.size()+wy2.size()<<"\n"; for (int j : wy1) cout<<j<<" "; for (int j : wy2) cout<<j<<" "; cout<<"\n"; wy1.clear(); wy2.clear(); for (i = 1; i < n+1; i++){ if (perm[i] > i){ wy1.push_back(i); wy2.push_back(perm[i]); } } reverse(wy2.begin(),wy2.end()); for (int j : wy2) wy1.push_back(j); cout<<wy1.size()<<"\n"; for (int j : wy1) cout<<j<<" "; cout<<"\n"; return 0; } |