#include <bits/stdc++.h> using namespace std; bool check(vector<int> heigh){ for(int i=1 ;i< heigh.size() ;i++){ if(heigh[i-1]>heigh[i]){ return false; } } return true; } int main(){ long long n; cin>>n; vector<int> heigh(n); vector<int> sorted(n); vector<int> zlicz(3003, -1); for(auto &a : heigh){ cin>>a; } for(int i=0; i<n; i++){ sorted[i]=heigh[i]; } sort(sorted.begin(), sorted.end()); //reverse(sorted.begin(), sorted.end()); for(int i=0; i<n; i++){ zlicz[sorted[i]]=i; } // return 0; vector<string> output1, output2; int it=0; while(!check(heigh)){ vector<int> resb; vector<int> rese; vector<int> vis(n); for(int i=0; i<n; i++){ if(vis[i]==false && vis[zlicz[heigh[i]]]==false && zlicz[heigh[i]]!=i){ resb.push_back(i); rese.push_back(zlicz[heigh[i]]); vis[zlicz[heigh[i]]]=true; vis[i]=true; } } output1.push_back(to_string(resb.size()*2)); output2.push_back(""); for(int i=0; i<resb.size(); i++){ int tmp=resb[i]+1; output2[it]+=to_string(tmp)+" "; } for(int i=rese.size()-1; i>=0 ;i--){ int tmp=rese[i]+1; output2[it]+=to_string(tmp)+" "; } for(int i=0 ;i<rese.size(); i++){ swap(heigh[resb[i]], heigh[rese[i]]); } it++; } cout<<output1.size()<<endl; for(int i=0; i<output1.size(); i++){ cout<<output1[i]<<endl; cout<<output2[i]<<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 | #include <bits/stdc++.h> using namespace std; bool check(vector<int> heigh){ for(int i=1 ;i< heigh.size() ;i++){ if(heigh[i-1]>heigh[i]){ return false; } } return true; } int main(){ long long n; cin>>n; vector<int> heigh(n); vector<int> sorted(n); vector<int> zlicz(3003, -1); for(auto &a : heigh){ cin>>a; } for(int i=0; i<n; i++){ sorted[i]=heigh[i]; } sort(sorted.begin(), sorted.end()); //reverse(sorted.begin(), sorted.end()); for(int i=0; i<n; i++){ zlicz[sorted[i]]=i; } // return 0; vector<string> output1, output2; int it=0; while(!check(heigh)){ vector<int> resb; vector<int> rese; vector<int> vis(n); for(int i=0; i<n; i++){ if(vis[i]==false && vis[zlicz[heigh[i]]]==false && zlicz[heigh[i]]!=i){ resb.push_back(i); rese.push_back(zlicz[heigh[i]]); vis[zlicz[heigh[i]]]=true; vis[i]=true; } } output1.push_back(to_string(resb.size()*2)); output2.push_back(""); for(int i=0; i<resb.size(); i++){ int tmp=resb[i]+1; output2[it]+=to_string(tmp)+" "; } for(int i=rese.size()-1; i>=0 ;i--){ int tmp=rese[i]+1; output2[it]+=to_string(tmp)+" "; } for(int i=0 ;i<rese.size(); i++){ swap(heigh[resb[i]], heigh[rese[i]]); } it++; } cout<<output1.size()<<endl; for(int i=0; i<output1.size(); i++){ cout<<output1[i]<<endl; cout<<output2[i]<<endl; } return 0; } |