#include <bits/stdc++.h> #include <queue> using namespace std; int n; int t[3001]; int a[3001]; int b[3001]; bool visit2[3001]; vector<int> sol[3001]; void read() { cin>>n; for(int i =0;i<n;i++){ cin>>t[i]; a[i]=t[i]; } sort(a,a+n); for(int i = 0;i<n;i++){ b[a[i]]=i; } for(int i = 0;i<n;i++){ t[i]=b[t[i]]; } /*for(int i = 0;i<n;i++){ cout<<t[i]; }*/ } int h[3001]; int h_size; int k = 0; void solve() { read(); while(true){ // mapuj for(int i =0;i<n;i++){ a[t[i]]=i; visit2[i]=false; } h_size = 0; //cout<<"\n"; for(int i = n-1;i>=0;i--){ if(t[i]!=i && !visit2[i] && !visit2[a[i]]){ h[h_size++]=i; h[h_size++]=a[i]; visit2[i] = true; visit2[a[i]] = true; // cout<<i+1<<" "<<a[i]+1<<"\n"; } } if(h_size==0)break; for(int i = 0; i < h_size;i+=2) { sol[k].push_back(h[i]); sol[k].push_back(h[i+1]); swap(t[h[i]],t[h[i+1]]); } k++; } cout<<k<<"\n"; for(int i =0;i<k;i++){ cout<<sol[i].size()<<"\n"; for(int j=0;j<sol[i].size();j+=2) cout<<sol[i][j]+1<<" "; for(int j=sol[i].size()-1;j>=0;j-=2) cout<<sol[i][j]+1<<" "; cout<<"\n"; } } int main() { ios_base::sync_with_stdio(false); solve(); 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 | #include <bits/stdc++.h> #include <queue> using namespace std; int n; int t[3001]; int a[3001]; int b[3001]; bool visit2[3001]; vector<int> sol[3001]; void read() { cin>>n; for(int i =0;i<n;i++){ cin>>t[i]; a[i]=t[i]; } sort(a,a+n); for(int i = 0;i<n;i++){ b[a[i]]=i; } for(int i = 0;i<n;i++){ t[i]=b[t[i]]; } /*for(int i = 0;i<n;i++){ cout<<t[i]; }*/ } int h[3001]; int h_size; int k = 0; void solve() { read(); while(true){ // mapuj for(int i =0;i<n;i++){ a[t[i]]=i; visit2[i]=false; } h_size = 0; //cout<<"\n"; for(int i = n-1;i>=0;i--){ if(t[i]!=i && !visit2[i] && !visit2[a[i]]){ h[h_size++]=i; h[h_size++]=a[i]; visit2[i] = true; visit2[a[i]] = true; // cout<<i+1<<" "<<a[i]+1<<"\n"; } } if(h_size==0)break; for(int i = 0; i < h_size;i+=2) { sol[k].push_back(h[i]); sol[k].push_back(h[i+1]); swap(t[h[i]],t[h[i+1]]); } k++; } cout<<k<<"\n"; for(int i =0;i<k;i++){ cout<<sol[i].size()<<"\n"; for(int j=0;j<sol[i].size();j+=2) cout<<sol[i][j]+1<<" "; for(int j=sol[i].size()-1;j>=0;j-=2) cout<<sol[i][j]+1<<" "; cout<<"\n"; } } int main() { ios_base::sync_with_stdio(false); solve(); return 0; } |