#include<bits/stdc++.h> using namespace std; bool odw[3001],bul; int n; pair<int,int>tab[3001]; int pol[3001],wst[3001]; vector<pair<int,int>>pary; void split(int w){ int v=wst[w]; if(!odw[v]){ odw[v]=1;odw[w]=1; pary.emplace_back(v,w); //cout<<v<<" "<<w<<"\n";; v=wst[v],w=pol[w]; while((v!=w)&&(!odw[v])&&(!odw[w])){ //cout<<v<<" "<<w<<"\n";; pary.emplace_back(v,w); odw[v]=1;odw[w]=1; v=wst[v];w=pol[w]; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; for(int i=0;i<n;++i){ cin>>tab[i].first; tab[i].second=i; } sort(tab,tab+n); for(int i=0;i<n;++i){ pol[tab[i].second]=i; wst[i]=tab[i].second; } for(int i=0;i<n;++i){ if(pol[i]!=i&&!odw[i]){ split(i); bul=1; } } if(!bul){ cout<<0<<"\n"; return 0; } bul=0; for(int i=0;i<(int)pary.size();++i){ swap(pol[pary[i].first],pol[pary[i].second]); } for(int i=0;i<n;++i){ if(pol[i]!=i)bul=1; } if(!bul){ cout<<1<<"\n"; cout<<pary.size()*2<<"\n"; for(int i=0;i<(int)pary.size();++i){ cout<<pary[i].first+1<<" "; } for(int i=pary.size()-1;i>=0;--i){ cout<<pary[i].second+1<<" "; } } else { cout<<2<<"\n"; cout<<pary.size()*2<<"\n"; for(int i=0;i<(int)pary.size();++i){ cout<<pary[i].first+1<<" "; } for(int i=pary.size()-1;i>=0;--i){ cout<<pary[i].second+1<<" "; } cout<<"\n"; pary.clear(); for(int i=0;i<n;++i)odw[i]=0; for(int i=0;i<n;++i){ if(pol[i]!=i&&!odw[i]){ pary.emplace_back(i,pol[i]); odw[i]=1;odw[pol[i]]=1; } } cout<<pary.size()*2<<"\n"; for(int i=0;i<(int)pary.size();++i){ cout<<pary[i].first+1<<" "; } for(int i=pary.size()-1;i>=0;--i){ cout<<pary[i].second+1<<" "; } } }
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 | #include<bits/stdc++.h> using namespace std; bool odw[3001],bul; int n; pair<int,int>tab[3001]; int pol[3001],wst[3001]; vector<pair<int,int>>pary; void split(int w){ int v=wst[w]; if(!odw[v]){ odw[v]=1;odw[w]=1; pary.emplace_back(v,w); //cout<<v<<" "<<w<<"\n";; v=wst[v],w=pol[w]; while((v!=w)&&(!odw[v])&&(!odw[w])){ //cout<<v<<" "<<w<<"\n";; pary.emplace_back(v,w); odw[v]=1;odw[w]=1; v=wst[v];w=pol[w]; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; for(int i=0;i<n;++i){ cin>>tab[i].first; tab[i].second=i; } sort(tab,tab+n); for(int i=0;i<n;++i){ pol[tab[i].second]=i; wst[i]=tab[i].second; } for(int i=0;i<n;++i){ if(pol[i]!=i&&!odw[i]){ split(i); bul=1; } } if(!bul){ cout<<0<<"\n"; return 0; } bul=0; for(int i=0;i<(int)pary.size();++i){ swap(pol[pary[i].first],pol[pary[i].second]); } for(int i=0;i<n;++i){ if(pol[i]!=i)bul=1; } if(!bul){ cout<<1<<"\n"; cout<<pary.size()*2<<"\n"; for(int i=0;i<(int)pary.size();++i){ cout<<pary[i].first+1<<" "; } for(int i=pary.size()-1;i>=0;--i){ cout<<pary[i].second+1<<" "; } } else { cout<<2<<"\n"; cout<<pary.size()*2<<"\n"; for(int i=0;i<(int)pary.size();++i){ cout<<pary[i].first+1<<" "; } for(int i=pary.size()-1;i>=0;--i){ cout<<pary[i].second+1<<" "; } cout<<"\n"; pary.clear(); for(int i=0;i<n;++i)odw[i]=0; for(int i=0;i<n;++i){ if(pol[i]!=i&&!odw[i]){ pary.emplace_back(i,pol[i]); odw[i]=1;odw[pol[i]]=1; } } cout<<pary.size()*2<<"\n"; for(int i=0;i<(int)pary.size();++i){ cout<<pary[i].first+1<<" "; } for(int i=pary.size()-1;i>=0;--i){ cout<<pary[i].second+1<<" "; } } } |