The questions of the postgraduates in Zhejiang University--The Order of Olympic Games

Source: Internet
Author: User

Clearly is a water problem!! There is no algorithm at all, simulation can be.

However, in doing this problem inadvertently reviewed the knowledge of the function object, so this question is still meaningful, and for C + + unfamiliar words,

It would be annoying to write.

Title Description:

According to the requirements, to the country ranking.

input:
There are multiple sets of data.


output:
Sort there are 4 ways: Gold medal total medal total gold medal population Proportion medal population proportion




Sample Input:
4 44 8 16 6 24 8 22 12 40 1 2 34 28 10 18 11 28 12 38 13 40 3
Sample output:
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 (); }}

The questions of the postgraduates in Zhejiang University--The Order of Olympic Games

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.