#include <bits/stdc++.h> using namespace std; int heightToPlace[3001]; int placeToHeight[3001]; int currentPlacement[3001]; unordered_map<int,bool> ifUsed[10000]; vector<int> answers1[10000]; vector<int> answers2[10000]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,round=0; bool x; cin>>n; for(int i=1;i<=n;i++){ cin>>placeToHeight[i]; currentPlacement[i]=placeToHeight[i]; } sort(placeToHeight,placeToHeight+(n+1)); for(int i=1;i<=n;i++){ heightToPlace[placeToHeight[i]]=i; } for(;;){ round++; x=0; for(int j=1;j<=n;j++){ if(ifUsed[j][round]==1) continue; if(currentPlacement[j]==placeToHeight[j]) continue; if(ifUsed[heightToPlace[currentPlacement[j]]][round]==1) continue; answers1[round].push_back(j); answers2[round].push_back(heightToPlace[currentPlacement[j]]); swap(currentPlacement[j],currentPlacement[heightToPlace[currentPlacement[j]]]); ifUsed[heightToPlace[currentPlacement[j]]][round]=1; ifUsed[j][round]=1; x=1; } if(x==0) break; } cout<<round-1<<'\n'; for(int i=1;i<round;i++){ cout<<2*answers1[i].size()<<'\n'; for(int j=0;j<answers1[i].size();j++){ cout<<answers1[i][j]<<" "; } for(int j=answers2[i].size()-1;j>=0;j--){ cout<<answers2[i][j]<<" "; } cout<<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 | #include <bits/stdc++.h> using namespace std; int heightToPlace[3001]; int placeToHeight[3001]; int currentPlacement[3001]; unordered_map<int,bool> ifUsed[10000]; vector<int> answers1[10000]; vector<int> answers2[10000]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,round=0; bool x; cin>>n; for(int i=1;i<=n;i++){ cin>>placeToHeight[i]; currentPlacement[i]=placeToHeight[i]; } sort(placeToHeight,placeToHeight+(n+1)); for(int i=1;i<=n;i++){ heightToPlace[placeToHeight[i]]=i; } for(;;){ round++; x=0; for(int j=1;j<=n;j++){ if(ifUsed[j][round]==1) continue; if(currentPlacement[j]==placeToHeight[j]) continue; if(ifUsed[heightToPlace[currentPlacement[j]]][round]==1) continue; answers1[round].push_back(j); answers2[round].push_back(heightToPlace[currentPlacement[j]]); swap(currentPlacement[j],currentPlacement[heightToPlace[currentPlacement[j]]]); ifUsed[heightToPlace[currentPlacement[j]]][round]=1; ifUsed[j][round]=1; x=1; } if(x==0) break; } cout<<round-1<<'\n'; for(int i=1;i<round;i++){ cout<<2*answers1[i].size()<<'\n'; for(int j=0;j<answers1[i].size();j++){ cout<<answers1[i][j]<<" "; } for(int j=answers2[i].size()-1;j>=0;j--){ cout<<answers2[i][j]<<" "; } cout<<endl; } return 0; } |