#include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> v(n); for(int& i : v) cin >> i; vector<int> xx = v; sort(xx.begin(), xx.end()); vector<int> mp(3001); for(int i = 0; i < n; i++) { mp[xx[i]] = i; } for(int& i : v) i = mp[i]; vector<int> u(n); vector<vector<int>> c; int mx = 1; for(int i = 0; i < n; i++) { if(u[i]) continue; int j = i; c.push_back(vector<int>()); do { u[j] = 1; c.back().push_back(j); j = v[j]; } while(j != i); mx = max(mx, (int)c.back().size()); } if(mx == 1) { cout << 0 << endl; } else if(mx == 2) { cout << 1 << endl; vector<int> a,b; for(int i = 0; i < c.size(); i++) { if(c[i].size() == 2) { a.push_back(c[i][0] + 1); b.push_back(c[i][1] + 1); } } cout << 2 * a.size() << endl; for(int i : a) cout << i << " "; reverse(b.begin(), b.end()); for(int i = 0; i < b.size(); i++) { cout << b[i]; if(i + 1 < b.size()) cout << " "; } cout << endl; } else { cout << 2 << endl; vector<pair<int,int>> x,y; for(int i = 0; i < c.size(); i++) { if(c[i].size() == 1) continue; x.push_back({c[i][c[i].size() - 2], c[i][c[i].size() - 1]}); for(int j = 0; 2 * j + 3 < c[i].size(); j++) { x.push_back({c[i][j], c[i][c[i].size() - 3 - j]}); } for(int j = 0; 2 * j + 2 < c[i].size(); j++) { y.push_back({c[i][j], c[i][c[i].size() - 2 - j]}); } } cout << 2 * x.size() << endl; for(auto [a,b] : x) cout << a + 1 << " "; for(int i = (int)x.size() - 1; i >= 0; i--) { cout << x[i].second + 1; if(i) cout << " "; } cout << endl << 2 * y.size() << endl; for(auto [a,b] : y) cout << a + 1 << " "; for(int i = (int)y.size() - 1; i >= 0; i--) { cout << y[i].second + 1; if(i) cout << " "; } 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | #include <bits/stdc++.h> #define dbg(x) " [" << #x << ": " << (x) << "] " using namespace std; template<typename A, typename B> ostream& operator<<(ostream& out, const pair<A,B>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<typename T> ostream& operator<<(ostream& out, const vector<T>& c) { out << "{"; for(auto it = c.begin(); it != c.end(); it++) { if(it != c.begin()) out << ", "; out << *it; } return out << "}"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> v(n); for(int& i : v) cin >> i; vector<int> xx = v; sort(xx.begin(), xx.end()); vector<int> mp(3001); for(int i = 0; i < n; i++) { mp[xx[i]] = i; } for(int& i : v) i = mp[i]; vector<int> u(n); vector<vector<int>> c; int mx = 1; for(int i = 0; i < n; i++) { if(u[i]) continue; int j = i; c.push_back(vector<int>()); do { u[j] = 1; c.back().push_back(j); j = v[j]; } while(j != i); mx = max(mx, (int)c.back().size()); } if(mx == 1) { cout << 0 << endl; } else if(mx == 2) { cout << 1 << endl; vector<int> a,b; for(int i = 0; i < c.size(); i++) { if(c[i].size() == 2) { a.push_back(c[i][0] + 1); b.push_back(c[i][1] + 1); } } cout << 2 * a.size() << endl; for(int i : a) cout << i << " "; reverse(b.begin(), b.end()); for(int i = 0; i < b.size(); i++) { cout << b[i]; if(i + 1 < b.size()) cout << " "; } cout << endl; } else { cout << 2 << endl; vector<pair<int,int>> x,y; for(int i = 0; i < c.size(); i++) { if(c[i].size() == 1) continue; x.push_back({c[i][c[i].size() - 2], c[i][c[i].size() - 1]}); for(int j = 0; 2 * j + 3 < c[i].size(); j++) { x.push_back({c[i][j], c[i][c[i].size() - 3 - j]}); } for(int j = 0; 2 * j + 2 < c[i].size(); j++) { y.push_back({c[i][j], c[i][c[i].size() - 2 - j]}); } } cout << 2 * x.size() << endl; for(auto [a,b] : x) cout << a + 1 << " "; for(int i = (int)x.size() - 1; i >= 0; i--) { cout << x[i].second + 1; if(i) cout << " "; } cout << endl << 2 * y.size() << endl; for(auto [a,b] : y) cout << a + 1 << " "; for(int i = (int)y.size() - 1; i >= 0; i--) { cout << y[i].second + 1; if(i) cout << " "; } cout << endl; } return 0; } |