#include <bits/stdc++.h> using namespace std; int n; pair<int, int> t2[3001]; int t[3001]; bool odw[3001]; vector<vector<int> > cyk; vector<int> swp1; vector<int> swp2; int main() { scanf("%d", &n); bool ok = 1; for(int i=0; i^n; ++i) { scanf("%d", &t2[i].first); if(i && t2[i] < t2[i-1]) ok = 0; t2[i].second = i; } if(ok) { printf("0\n"); return 0; } sort(t2, t2+n); for(int i=0; i^n; ++i) { t[t2[i].second] = i; } for(int i=0; i^n; ++i) { if(odw[i]) continue; vector<int> v; v.push_back(i); odw[i] = 1; int w = t[i]; while(w != i) { odw[w] = 1; v.push_back(w); w = t[w]; } cyk.push_back(v); } bool fin = 1; for(int i=0; i^cyk.size(); ++i) { if(cyk[i].size() == 1) continue; if(cyk[i].size() >= 3) fin = 0; for(int j=0; j < cyk[i].size()/2; ++j) { swp1.push_back(cyk[i][j]); swp2.push_back(cyk[i][cyk[i].size() - 1 - j - (cyk[i].size() % 2)]); } } if(fin) printf("1\n"); else printf("2\n"); printf("%ld\n", swp1.size() * 2); for(auto e:swp1) printf("%d ", e+1); reverse(swp2.begin(), swp2.end()); for(auto e:swp2) printf("%d ", e+1); printf("\n"); if(fin) return 0; swp1.clear(); swp2.clear(); for(int i=0; i^cyk.size(); ++i) { if(cyk[i].size() <= 2) continue; if(cyk[i].size() % 2) { for(int j=0; j<cyk[i].size()/2; ++j) { swp1.push_back(cyk[i][j]); swp2.push_back(cyk[i][cyk[i].size() - 1 - j]); } } else { for(int j=1; j<cyk[i].size()/2; ++j) { swp1.push_back(cyk[i][j]); swp2.push_back(cyk[i][cyk[i].size() - j]); } } } printf("%ld\n", swp1.size() * 2); for(auto e:swp1) printf("%d ", e+1); reverse(swp2.begin(), swp2.end()); for(auto e:swp2) printf("%d ", e+1); printf("\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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #include <bits/stdc++.h> using namespace std; int n; pair<int, int> t2[3001]; int t[3001]; bool odw[3001]; vector<vector<int> > cyk; vector<int> swp1; vector<int> swp2; int main() { scanf("%d", &n); bool ok = 1; for(int i=0; i^n; ++i) { scanf("%d", &t2[i].first); if(i && t2[i] < t2[i-1]) ok = 0; t2[i].second = i; } if(ok) { printf("0\n"); return 0; } sort(t2, t2+n); for(int i=0; i^n; ++i) { t[t2[i].second] = i; } for(int i=0; i^n; ++i) { if(odw[i]) continue; vector<int> v; v.push_back(i); odw[i] = 1; int w = t[i]; while(w != i) { odw[w] = 1; v.push_back(w); w = t[w]; } cyk.push_back(v); } bool fin = 1; for(int i=0; i^cyk.size(); ++i) { if(cyk[i].size() == 1) continue; if(cyk[i].size() >= 3) fin = 0; for(int j=0; j < cyk[i].size()/2; ++j) { swp1.push_back(cyk[i][j]); swp2.push_back(cyk[i][cyk[i].size() - 1 - j - (cyk[i].size() % 2)]); } } if(fin) printf("1\n"); else printf("2\n"); printf("%ld\n", swp1.size() * 2); for(auto e:swp1) printf("%d ", e+1); reverse(swp2.begin(), swp2.end()); for(auto e:swp2) printf("%d ", e+1); printf("\n"); if(fin) return 0; swp1.clear(); swp2.clear(); for(int i=0; i^cyk.size(); ++i) { if(cyk[i].size() <= 2) continue; if(cyk[i].size() % 2) { for(int j=0; j<cyk[i].size()/2; ++j) { swp1.push_back(cyk[i][j]); swp2.push_back(cyk[i][cyk[i].size() - 1 - j]); } } else { for(int j=1; j<cyk[i].size()/2; ++j) { swp1.push_back(cyk[i][j]); swp2.push_back(cyk[i][cyk[i].size() - j]); } } } printf("%ld\n", swp1.size() * 2); for(auto e:swp1) printf("%d ", e+1); reverse(swp2.begin(), swp2.end()); for(auto e:swp2) printf("%d ", e+1); printf("\n"); } |