#include<bits/stdc++.h> using namespace std; int tab[3005]; int tab2[3005]; int tabsort[3005]; int tabwsp[3005]; int tabzli[3005]; queue<int> tabzam1; queue<int> tabzam2; int tabodp[3005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n,zli=1,ile=0; cin>>n; for(int i=1;i<=n;i++) { cin>>tab[i]; tab2[i]=tab[i]; tabsort[i]=tab[i]; } sort(tabsort+1,tabsort+n+1); for(int i=1;i<=n;i++) { tabwsp[tabsort[i]]=i; //cout<<tabwsp[tabsort[i]]<<" "; } //cout<<endl; while(zli!=0) { zli=0; ile++; for(int i=1;i<=n;i++) { if(i!=tabwsp[tab[i]]) { if(tabzli[i]==0) { if(tabzli[tabwsp[tab[i]]]==0) { zli++; tabzam1.push(i); tabzam2.push(tabwsp[tab[i]]); //cout<<zli<<endl; //cout<<i<<" "<<tabwsp[tab[i]]<<endl; tabzli[i]++; tabzli[tabwsp[tab[i]]]++; } } } } int h,g,z; if(zli==0) { continue; } //cout<<zli<<endl; while(!tabzam1.empty()) { h=tabzam1.front(); g=tabzam2.front(); //cout<<tabod[ile]<<endl; //cout<<h<<" "<<g<<endl; tabzam1.pop(); tabzam2.pop(); tabzli[h]--; tabzli[g]--; z=tab[h]; tab[h]=tab[g]; tab[g]=z; } } cout<<ile-1<<'\n'; zli=1; while(zli!=0) { zli=0; for(int i=1;i<=n;i++) { if(i!=tabwsp[tab2[i]]) { if(tabzli[i]==0) { if(tabzli[tabwsp[tab2[i]]]==0) { zli++; tabzam1.push(i); tabzam2.push(tabwsp[tab2[i]]); //cout<<zli<<endl; //cout<<i<<" "<<tabwsp[tab2[i]]<<endl; tabzli[i]++; tabzli[tabwsp[tab2[i]]]++; } } } } int h,g,z; if(zli==0) { continue; } //cout<<zli<<endl; int j=zli*2; while(!tabzam1.empty()) { h=tabzam1.front(); g=tabzam2.front(); //cout<<tabod[ile]<<endl; //cout<<h<<" "<<g<<endl; tabodp[(zli*2)-j+1]=h; tabodp[j]=g; tabzam1.pop(); tabzam2.pop(); tabzli[h]--; tabzli[g]--; z=tab2[h]; tab2[h]=tab2[g]; tab2[g]=z; j--; } cout<<zli*2<<'\n'; for(int j=1;j<=zli*2;j++) { cout<<tabodp[j]<<" "; } cout<<'\n'; } }
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | #include<bits/stdc++.h> using namespace std; int tab[3005]; int tab2[3005]; int tabsort[3005]; int tabwsp[3005]; int tabzli[3005]; queue<int> tabzam1; queue<int> tabzam2; int tabodp[3005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n,zli=1,ile=0; cin>>n; for(int i=1;i<=n;i++) { cin>>tab[i]; tab2[i]=tab[i]; tabsort[i]=tab[i]; } sort(tabsort+1,tabsort+n+1); for(int i=1;i<=n;i++) { tabwsp[tabsort[i]]=i; //cout<<tabwsp[tabsort[i]]<<" "; } //cout<<endl; while(zli!=0) { zli=0; ile++; for(int i=1;i<=n;i++) { if(i!=tabwsp[tab[i]]) { if(tabzli[i]==0) { if(tabzli[tabwsp[tab[i]]]==0) { zli++; tabzam1.push(i); tabzam2.push(tabwsp[tab[i]]); //cout<<zli<<endl; //cout<<i<<" "<<tabwsp[tab[i]]<<endl; tabzli[i]++; tabzli[tabwsp[tab[i]]]++; } } } } int h,g,z; if(zli==0) { continue; } //cout<<zli<<endl; while(!tabzam1.empty()) { h=tabzam1.front(); g=tabzam2.front(); //cout<<tabod[ile]<<endl; //cout<<h<<" "<<g<<endl; tabzam1.pop(); tabzam2.pop(); tabzli[h]--; tabzli[g]--; z=tab[h]; tab[h]=tab[g]; tab[g]=z; } } cout<<ile-1<<'\n'; zli=1; while(zli!=0) { zli=0; for(int i=1;i<=n;i++) { if(i!=tabwsp[tab2[i]]) { if(tabzli[i]==0) { if(tabzli[tabwsp[tab2[i]]]==0) { zli++; tabzam1.push(i); tabzam2.push(tabwsp[tab2[i]]); //cout<<zli<<endl; //cout<<i<<" "<<tabwsp[tab2[i]]<<endl; tabzli[i]++; tabzli[tabwsp[tab2[i]]]++; } } } } int h,g,z; if(zli==0) { continue; } //cout<<zli<<endl; int j=zli*2; while(!tabzam1.empty()) { h=tabzam1.front(); g=tabzam2.front(); //cout<<tabod[ile]<<endl; //cout<<h<<" "<<g<<endl; tabodp[(zli*2)-j+1]=h; tabodp[j]=g; tabzam1.pop(); tabzam2.pop(); tabzli[h]--; tabzli[g]--; z=tab2[h]; tab2[h]=tab2[g]; tab2[g]=z; j--; } cout<<zli*2<<'\n'; for(int j=1;j<=zli*2;j++) { cout<<tabodp[j]<<" "; } cout<<'\n'; } } |