1:31:12:11:21:11:1
The idea is to look at each country as a structure, sort each of the characteristics of the structure, and then count the
Rank more in the top, note that if the number of gold medals is the same 100 90 90 80, the ranking is 1,2,2,4; for the same ranking two sort methods,
Total Gold medal < Total medals < gold medal population ratio < medal population ratio, that is, in the process of ranking as the number of gold medals is 1,
The number of medals in the process of ranking is also ranked as 1, then the gold medal is the key to use the way.
If the order is forced to write, it will appear to write a lot of sort, we have the sort of CMP function as a function object in the form, can be implemented according to
Different key sort;
structcmp{intcomp; CMP (intx): Comp (x) {}; BOOL operator() (ConstNode &a,ConstNode &b)Const { if(comp==1)returnA.C.G_MD >B.C.G_MD; Else if(Comp = =2)returnA.c.md >b.c.md; Else if(Comp = =3)returnA.C.PG >b.c.pg; Else returna.c.pm >b.c.pm; }};
Here, the CMP is a class that overloads the function call budget symbol, if we live an object: CMP A (1), you can pass a to the sort function, he will automatically
Sort by the way we agreed. For details, see "C++primer 5th" heavy-duty operation and type conversion section 14.8;
#include <iostream>#include<algorithm>#include<vector>using namespacestd;Const intINF =1000000;Const intMAXN = 1e5 +5;intn,m;structcountry//Country {intG_md,md,y,peo; Doublepg,pm; Country () {} country (intXintYintz): G_MD (x), MD (y), PEO (z) {PG= g_md*1.0/PEO; PM= md*1.0/PEO; }};structnode//Country + number {intID; Country C; Node () {} node (intI,country &RHS): ID (i), C (RHS) {}};structans//The best results of the first I countries and what sort of methods to use {intPOS, kind; Ans () {pos=inf;} Ans (intXinty):p os (x), kind (y) {}};vector<country>Vc;vector<node>V;vector<Ans>ans;//the answer to outputstructcmp{intcomp; CMP (intx): Comp (x) {}; BOOL operator() (ConstNode &a,ConstNode &b)Const { if(comp==1)returnA.C.G_MD >B.C.G_MD; Else if(Comp = =2)returnA.c.md >b.c.md; Else if(Comp = =3)returnA.C.PG >b.c.pg; Else returna.c.pm >b.c.pm; }};BOOLEqual (country &p,country &q,intk) { if(k = =1)returnP.G_MD = =Q.G_MD; if(k = =2)returnP.md = =q.md; if(k = =3)returnP.PG = =q.pg; if(k = =4)returnP.PM = =q.pm;}voidUpdate (vector<node>& V,intkind)//Beware of elements with the same key value to special handling {intPS =1; if(ans[v[0].id].pos >PS) ans[v[0].id]=Ans (Ps,kind); for(inti =1; I < v.size (); ++i) {if(!equal (v[i-1].c,v[i].c,kind) PS = i+1; if(Ans[v[i].id].pos >PS) ans[v[i].id]=Ans (Ps,kind); }}intMain () { for(; Cin >> N >>m;) {intx, y, Z; Ans.resize (m);//reset vector size, resize function for(inti =0; I < n; ++i) {cin>> x>> y>>Z; Vc.push_back (Country (x, Y, z)); } intID; for(inti =0; I < m; ++i) {cin>>ID; V.push_back (Node (i,vc[id)); } for(inti =1; I <=4; ++i) {Sort (V.begin (), V.end (), CMP (i)); Update (V,I); } for(inti =0; I < m; ++i) {cout<< Ans[i].pos <<':'<< ans[i].kind<<Endl; } cout<<Endl; V.clear (); Vc.clear (); Ans.clear (); }}