//Franciszek Witt #include<bits/stdc++.h> using namespace std; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif typedef long long ll; struct ska { vector<int> ve; void add(int x) { ve.emplace_back(x); } void go() { sort(ve.begin(), ve.end()); } int get(int x) { return lower_bound(ve.begin(), ve.end(), x) - ve.begin() + 1; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> v(n + 1); ska sk; FOR(i, 1, n) { cin >> v[i]; sk.add(v[i]); } sk.go(); FOR(i, 1, n) v[i] = sk.get(v[i]); vector bn(n + 1, 0); vector ccl(0, vector<int>()); FOR(i, 1, n) { if(!bn[i]) { ccl.resize(ccl.size() + 1); int ptr = i; while(!bn[ptr]) { bn[ptr] = 1; ccl.back().emplace_back(ptr); ptr = v[ptr]; } } } int mxs = 0; for(auto &i : ccl) mxs = max(mxs, ssize(i)); auto work = [&] (vector<pair<int, int>> ve) { vector<int> le, ri; for(auto [l, r] : ve) { le.emplace_back(l); ri.emplace_back(r); } reverse(ri.begin(), ri.end()); for(auto i : ri) le.emplace_back(i); return le; }; debug(v); debug(ccl); if(mxs == 1) { cout << 0 << endl; } else if(mxs == 2) { vector<pair<int, int>> vp; for(auto &i : ccl) if(ssize(i) == 2) vp.emplace_back(i[0], i[1]); auto tmp = work(vp); cout << 1 << endl; cout << ssize(tmp) << endl; for(auto i : tmp) cout << i << " "; cout << endl; } else { vector<pair<int, int>> vp0, vp1; for(auto &i : ccl) { int si = ssize(i); for(int j = 0; j < (si - 1) / 2; ++j) vp0.emplace_back(i[j], i[si - 2 - j]); for(int j = 0; j < si / 2; ++j) vp1.emplace_back(i[j], i[si - 1 - j]); } auto tmp0 = work(vp0), tmp1 = work(vp1); cout << 2 << endl; cout << ssize(tmp0) << endl; for(auto i : tmp0) cout << i << " "; cout << endl; cout << ssize(tmp1) << endl; for(auto i : tmp1) cout << i << " "; cout << endl; } }
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 112 | //Franciszek Witt #include<bits/stdc++.h> using namespace std; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif typedef long long ll; struct ska { vector<int> ve; void add(int x) { ve.emplace_back(x); } void go() { sort(ve.begin(), ve.end()); } int get(int x) { return lower_bound(ve.begin(), ve.end(), x) - ve.begin() + 1; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> v(n + 1); ska sk; FOR(i, 1, n) { cin >> v[i]; sk.add(v[i]); } sk.go(); FOR(i, 1, n) v[i] = sk.get(v[i]); vector bn(n + 1, 0); vector ccl(0, vector<int>()); FOR(i, 1, n) { if(!bn[i]) { ccl.resize(ccl.size() + 1); int ptr = i; while(!bn[ptr]) { bn[ptr] = 1; ccl.back().emplace_back(ptr); ptr = v[ptr]; } } } int mxs = 0; for(auto &i : ccl) mxs = max(mxs, ssize(i)); auto work = [&] (vector<pair<int, int>> ve) { vector<int> le, ri; for(auto [l, r] : ve) { le.emplace_back(l); ri.emplace_back(r); } reverse(ri.begin(), ri.end()); for(auto i : ri) le.emplace_back(i); return le; }; debug(v); debug(ccl); if(mxs == 1) { cout << 0 << endl; } else if(mxs == 2) { vector<pair<int, int>> vp; for(auto &i : ccl) if(ssize(i) == 2) vp.emplace_back(i[0], i[1]); auto tmp = work(vp); cout << 1 << endl; cout << ssize(tmp) << endl; for(auto i : tmp) cout << i << " "; cout << endl; } else { vector<pair<int, int>> vp0, vp1; for(auto &i : ccl) { int si = ssize(i); for(int j = 0; j < (si - 1) / 2; ++j) vp0.emplace_back(i[j], i[si - 2 - j]); for(int j = 0; j < si / 2; ++j) vp1.emplace_back(i[j], i[si - 1 - j]); } auto tmp0 = work(vp0), tmp1 = work(vp1); cout << 2 << endl; cout << ssize(tmp0) << endl; for(auto i : tmp0) cout << i << " "; cout << endl; cout << ssize(tmp1) << endl; for(auto i : tmp1) cout << i << " "; cout << endl; } } |