#include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); int n; cin>>n; vector<int> tab; vector<int> conv; for(int i = 0; i < n; i++){ int a; cin>>a; tab.push_back(a); conv.push_back(a); } sort(conv.begin(), conv.end()); for(int i = 0; i < n; i++){ tab[i] = lower_bound(conv.begin(), conv.end(), tab[i])-conv.begin(); } vector<vector<int>> ciagi; vector<bool> odw(n); for(int i = 0; i < n; i++){ if(!odw[i]){ ciagi.push_back(vector<int>()); int pt = i; while(!odw[pt]){ ciagi.back().push_back(pt+1); odw[pt] = true; pt = tab[pt]; } } } vector<pair<int, int>> ruch1, ruch2; for(int i = 0; i < ciagi.size(); i++){ if(ciagi[i].size()==2){ ruch1.push_back(make_pair(ciagi[i][0], ciagi[i][1])); } if(ciagi[i].size()>2){ for(int j = 0; j < (ciagi[i].size()-1)/2; j++){ ruch1.push_back({ciagi[i][j], ciagi[i][ciagi[i].size()-2-j]}); } for(int j = 0; j < ciagi[i].size()/2; j++){ ruch2.push_back({ciagi[i][j], ciagi[i][ciagi[i].size()-1-j]}); } } } if(ruch2.size()){ cout<<2<<'\n'; vector<int> v1, v2; for(int i = 0; i < ruch1.size(); i++){ v1.push_back(ruch1[i].first); v2.push_back(ruch1[i].second); } cout<<ruch1.size()*2<<'\n'; reverse(v2.begin(), v2.end()); for(auto it:v1) cout<<it<<" "; for(auto it:v2) cout<<it<<" "; cout<<'\n'; v1.resize(0), v2.resize(0); for(int i = 0; i < ruch2.size(); i++){ v1.push_back(ruch2[i].first); v2.push_back(ruch2[i].second); } cout<<ruch2.size()*2<<'\n'; reverse(v2.begin(), v2.end()); for(auto it:v1) cout<<it<<" "; for(auto it:v2) cout<<it<<" "; cout<<'\n'; }else if(ruch1.size()){ cout<<1<<'\n'; vector<int> v1, v2; for(int i = 0; i < ruch1.size(); i++){ v1.push_back(ruch1[i].first); v2.push_back(ruch1[i].second); } cout<<ruch1.size()*2<<'\n'; reverse(v2.begin(), v2.end()); for(auto it:v1) cout<<it<<" "; for(auto it:v2) cout<<it<<" "; cout<<'\n'; }else cout<<0<<'\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 | #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); int n; cin>>n; vector<int> tab; vector<int> conv; for(int i = 0; i < n; i++){ int a; cin>>a; tab.push_back(a); conv.push_back(a); } sort(conv.begin(), conv.end()); for(int i = 0; i < n; i++){ tab[i] = lower_bound(conv.begin(), conv.end(), tab[i])-conv.begin(); } vector<vector<int>> ciagi; vector<bool> odw(n); for(int i = 0; i < n; i++){ if(!odw[i]){ ciagi.push_back(vector<int>()); int pt = i; while(!odw[pt]){ ciagi.back().push_back(pt+1); odw[pt] = true; pt = tab[pt]; } } } vector<pair<int, int>> ruch1, ruch2; for(int i = 0; i < ciagi.size(); i++){ if(ciagi[i].size()==2){ ruch1.push_back(make_pair(ciagi[i][0], ciagi[i][1])); } if(ciagi[i].size()>2){ for(int j = 0; j < (ciagi[i].size()-1)/2; j++){ ruch1.push_back({ciagi[i][j], ciagi[i][ciagi[i].size()-2-j]}); } for(int j = 0; j < ciagi[i].size()/2; j++){ ruch2.push_back({ciagi[i][j], ciagi[i][ciagi[i].size()-1-j]}); } } } if(ruch2.size()){ cout<<2<<'\n'; vector<int> v1, v2; for(int i = 0; i < ruch1.size(); i++){ v1.push_back(ruch1[i].first); v2.push_back(ruch1[i].second); } cout<<ruch1.size()*2<<'\n'; reverse(v2.begin(), v2.end()); for(auto it:v1) cout<<it<<" "; for(auto it:v2) cout<<it<<" "; cout<<'\n'; v1.resize(0), v2.resize(0); for(int i = 0; i < ruch2.size(); i++){ v1.push_back(ruch2[i].first); v2.push_back(ruch2[i].second); } cout<<ruch2.size()*2<<'\n'; reverse(v2.begin(), v2.end()); for(auto it:v1) cout<<it<<" "; for(auto it:v2) cout<<it<<" "; cout<<'\n'; }else if(ruch1.size()){ cout<<1<<'\n'; vector<int> v1, v2; for(int i = 0; i < ruch1.size(); i++){ v1.push_back(ruch1[i].first); v2.push_back(ruch1[i].second); } cout<<ruch1.size()*2<<'\n'; reverse(v2.begin(), v2.end()); for(auto it:v1) cout<<it<<" "; for(auto it:v2) cout<<it<<" "; cout<<'\n'; }else cout<<0<<'\n'; return 0; } |