#include <cstdio> #include <vector> #include <list> #include <algorithm> using namespace std; class pii { public: int N,V,F; pii() {N = 0; V = 0; F = 0;} pii(int n, int v) {N = n; V = v; F = 0;} pii(const pii& a) {N = a.N; V = a.V; F = a.F;} pii operator=(const pii& a) {N = a.N; V = a.V; F = a.F; return *this;} int operator<(const pii& a) {return V < a.V;} }; int main() { int N,n,tmp,m; pii ptmp; vector<pii> H,sH; vector<int> Pl,Pr; vector< vector <int> > R; scanf("%d",&N); for (n = 0; n < N; n++) { scanf("%d",&tmp); H.push_back(pii(n,tmp)); } do { sH = H; sort(sH.begin(),sH.end()); Pl.clear(); Pr.clear(); for (n = 0; n < N; n++) { if (H[n].F == 0 && H[sH[n].N].F == 0 && sH[n].V != H[n].V) { Pl.push_back(n); Pr.push_back(sH[n].N); H[n].F = 1; H[sH[n].N].F = 1; } } for (n = 0; n < Pl.size(); n++) { ptmp = H[Pl[n]]; H[Pl[n]] = H[Pr[n]]; H[Pr[n]] = ptmp; H[Pl[n]].F = 0; H[Pl[n]].N = Pl[n]; H[Pr[n]].F = 0; H[Pr[n]].N = Pr[n]; } if (!Pr.empty()) { for (n = 0; n < Pr.size(); n++) Pl.push_back(Pr[Pr.size()-n-1]);//printf("%d ",Pr[Pr.size()-n-1]+1); R.push_back(Pl); } } while (!Pr.empty()); // for (vector<pii>::iterator it = H.begin(); it != H.end(); ++it) printf("%d ",it->V); // for (vector<pii>::iterator it = H.begin(); it != H.end(); ++it) printf("%d %d %d\n",it->N+1,it->V,it->F); // printf("\n"); printf("%d\n",R.size()); for (n = 0; n < R.size(); n++) { printf("%d\n",R[n].size()); for (m = 0; m < R[n].size(); m++) printf("%d ",R[n][m]+1); printf("\n"); } 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 78 79 80 81 82 83 84 | #include <cstdio> #include <vector> #include <list> #include <algorithm> using namespace std; class pii { public: int N,V,F; pii() {N = 0; V = 0; F = 0;} pii(int n, int v) {N = n; V = v; F = 0;} pii(const pii& a) {N = a.N; V = a.V; F = a.F;} pii operator=(const pii& a) {N = a.N; V = a.V; F = a.F; return *this;} int operator<(const pii& a) {return V < a.V;} }; int main() { int N,n,tmp,m; pii ptmp; vector<pii> H,sH; vector<int> Pl,Pr; vector< vector <int> > R; scanf("%d",&N); for (n = 0; n < N; n++) { scanf("%d",&tmp); H.push_back(pii(n,tmp)); } do { sH = H; sort(sH.begin(),sH.end()); Pl.clear(); Pr.clear(); for (n = 0; n < N; n++) { if (H[n].F == 0 && H[sH[n].N].F == 0 && sH[n].V != H[n].V) { Pl.push_back(n); Pr.push_back(sH[n].N); H[n].F = 1; H[sH[n].N].F = 1; } } for (n = 0; n < Pl.size(); n++) { ptmp = H[Pl[n]]; H[Pl[n]] = H[Pr[n]]; H[Pr[n]] = ptmp; H[Pl[n]].F = 0; H[Pl[n]].N = Pl[n]; H[Pr[n]].F = 0; H[Pr[n]].N = Pr[n]; } if (!Pr.empty()) { for (n = 0; n < Pr.size(); n++) Pl.push_back(Pr[Pr.size()-n-1]);//printf("%d ",Pr[Pr.size()-n-1]+1); R.push_back(Pl); } } while (!Pr.empty()); // for (vector<pii>::iterator it = H.begin(); it != H.end(); ++it) printf("%d ",it->V); // for (vector<pii>::iterator it = H.begin(); it != H.end(); ++it) printf("%d %d %d\n",it->N+1,it->V,it->F); // printf("\n"); printf("%d\n",R.size()); for (n = 0; n < R.size(); n++) { printf("%d\n",R[n].size()); for (m = 0; m < R[n].size(); m++) printf("%d ",R[n][m]+1); printf("\n"); } return 0; } |