// Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif typedef long long ll; const int inf = 1e9 + 7; int n; vector<int> vec; void rescale() { vector<pair<int,int>> sorted(n); for(int i=0;i<n;i++) { sorted[i] = {vec[i], i}; } sort(sorted.begin(), sorted.end()); for(int i=0;i<n;i++) { vec[sorted[i].second] = i; } } vector<int> positions() { vector<int> res(n); for(int i=0;i<n;i++) { res[vec[i]] = i; } return res; } vector<vector<int>> cycles; vector<bool> vis; void find_cycle(int v, int start) { vis[v] = true; cycles.back().push_back(v); if(vec[v] == start) return; find_cycle(vec[v], start); } void invert() { vis.assign(n, 0); for(int i=0;i<n;i++) { if(!vis[i]) { cycles.push_back({}); find_cycle(i, i); } } } void print_cycles() { vector<int> pos = positions(); int s = 0; for(int i=0;i<(int)cycles.size();i++) { s += ((int)cycles[i].size())/2 * 2; } cout<<s<<'\n'; for(int i=0;i<(int)cycles.size();i++) { for(int j=0;j<(int)cycles[i].size()/2;j++) cout<<pos[cycles[i][j]]+1<<' '; } for(int i=cycles.size()-1;i>=0;i--) { for(int j=((int)cycles[i].size()+1)/2;j<(int)cycles[i].size();j++) cout<<pos[cycles[i][j]]+1<<' '; } cout<<'\n'; } void apply() { vector<int> pos = positions(); for(int i=0;i<(int)cycles.size();i++) { for(int j=0;j<(int)cycles[i].size()/2;j++) swap(vec[pos[cycles[i][j]]], vec[pos[cycles[i][(int)cycles[i].size() - j-1]]]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; vec.resize(n); for(auto &v : vec) cin>>v; rescale(); dbg(vec); invert(); dbg(cycles); int maxi = 0; for(int i=0;i<(int)cycles.size();i++) { maxi = max(maxi, (int)cycles[i].size()); } dbg(maxi); if(maxi <= 1) { cout<<"0\n"; return 0; } if(maxi <= 2) { cout<<"1\n"; print_cycles(); return 0; } cout<<"2\n"; print_cycles(); apply(); dbg(vec); cycles.clear(); invert(); dbg(cycles); print_cycles(); }
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | // Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif typedef long long ll; const int inf = 1e9 + 7; int n; vector<int> vec; void rescale() { vector<pair<int,int>> sorted(n); for(int i=0;i<n;i++) { sorted[i] = {vec[i], i}; } sort(sorted.begin(), sorted.end()); for(int i=0;i<n;i++) { vec[sorted[i].second] = i; } } vector<int> positions() { vector<int> res(n); for(int i=0;i<n;i++) { res[vec[i]] = i; } return res; } vector<vector<int>> cycles; vector<bool> vis; void find_cycle(int v, int start) { vis[v] = true; cycles.back().push_back(v); if(vec[v] == start) return; find_cycle(vec[v], start); } void invert() { vis.assign(n, 0); for(int i=0;i<n;i++) { if(!vis[i]) { cycles.push_back({}); find_cycle(i, i); } } } void print_cycles() { vector<int> pos = positions(); int s = 0; for(int i=0;i<(int)cycles.size();i++) { s += ((int)cycles[i].size())/2 * 2; } cout<<s<<'\n'; for(int i=0;i<(int)cycles.size();i++) { for(int j=0;j<(int)cycles[i].size()/2;j++) cout<<pos[cycles[i][j]]+1<<' '; } for(int i=cycles.size()-1;i>=0;i--) { for(int j=((int)cycles[i].size()+1)/2;j<(int)cycles[i].size();j++) cout<<pos[cycles[i][j]]+1<<' '; } cout<<'\n'; } void apply() { vector<int> pos = positions(); for(int i=0;i<(int)cycles.size();i++) { for(int j=0;j<(int)cycles[i].size()/2;j++) swap(vec[pos[cycles[i][j]]], vec[pos[cycles[i][(int)cycles[i].size() - j-1]]]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; vec.resize(n); for(auto &v : vec) cin>>v; rescale(); dbg(vec); invert(); dbg(cycles); int maxi = 0; for(int i=0;i<(int)cycles.size();i++) { maxi = max(maxi, (int)cycles[i].size()); } dbg(maxi); if(maxi <= 1) { cout<<"0\n"; return 0; } if(maxi <= 2) { cout<<"1\n"; print_cycles(); return 0; } cout<<"2\n"; print_cycles(); apply(); dbg(vec); cycles.clear(); invert(); dbg(cycles); print_cycles(); } |