#include <bits/stdc++.h> using namespace std; int n; int h[3003]; struct x { int val; int id; int sid; }; x sh[3003]; bool valsort(const x &a, const x &b){ return(a.val<b.val); } bool idsort(const x &a, const x &b){ return(a.id<b.id); } vector<int>odp[3000]; int odps[3000]; int main() { ios_base::sync_with_stdio(0);cin.tie(0); cin>>n; for(int i=0; i<n; i++) { cin>>h[i]; sh[i].val = h[i]; sh[i].id = i; } sort(sh,sh+n,valsort); for(int i=0; i<n; i++) sh[i].sid = i; // sort(sh,sh+n,idsort); int counter = 0; while(1) { bool odw[3003]; for(int i=0; i<n; i++) odw[i] = 0; deque<int> Q; for(int i=0; i<n; i++) { if(h[i] != sh[i].val && !odw[i]) { odw[i] = true; odw[sh[i].id] = true; Q.push_front(i); Q.push_back(sh[i].id); swap(h[i],h[sh[i].id]); } } if(Q.empty())break; odps[counter] = Q.size(); //cout<<Q.size()<<'\n'; while(Q.size()) { //cout<<Q.front()+1<<' '; odp[counter].push_back(Q.front()+1); Q.pop_front(); } counter++; } int j = 0; cout<<counter<<'\n'; while(1) { if(odps[j] == 0) return 0; cout<<odps[j]<<'\n'; for(auto &w : odp[j]) cout<<w<<' '; cout<<'\n'; j++; } //for(int i=0; i<n; i++) cout<<sh[i].val<<','<<sh[i].id<<','<<sh[i].sid<<' '; }
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 | #include <bits/stdc++.h> using namespace std; int n; int h[3003]; struct x { int val; int id; int sid; }; x sh[3003]; bool valsort(const x &a, const x &b){ return(a.val<b.val); } bool idsort(const x &a, const x &b){ return(a.id<b.id); } vector<int>odp[3000]; int odps[3000]; int main() { ios_base::sync_with_stdio(0);cin.tie(0); cin>>n; for(int i=0; i<n; i++) { cin>>h[i]; sh[i].val = h[i]; sh[i].id = i; } sort(sh,sh+n,valsort); for(int i=0; i<n; i++) sh[i].sid = i; // sort(sh,sh+n,idsort); int counter = 0; while(1) { bool odw[3003]; for(int i=0; i<n; i++) odw[i] = 0; deque<int> Q; for(int i=0; i<n; i++) { if(h[i] != sh[i].val && !odw[i]) { odw[i] = true; odw[sh[i].id] = true; Q.push_front(i); Q.push_back(sh[i].id); swap(h[i],h[sh[i].id]); } } if(Q.empty())break; odps[counter] = Q.size(); //cout<<Q.size()<<'\n'; while(Q.size()) { //cout<<Q.front()+1<<' '; odp[counter].push_back(Q.front()+1); Q.pop_front(); } counter++; } int j = 0; cout<<counter<<'\n'; while(1) { if(odps[j] == 0) return 0; cout<<odps[j]<<'\n'; for(auto &w : odp[j]) cout<<w<<' '; cout<<'\n'; j++; } //for(int i=0; i<n; i++) cout<<sh[i].val<<','<<sh[i].id<<','<<sh[i].sid<<' '; } |