UVa207 - PGA Tour Prize Money

来源:互联网 发布:动力节点java视频网盘 编辑:程序博客网 时间:2024/05/16 03:39
#include<iostream>#include<iomanip>#include<sstream>#include<vector>#include<cstdio>#include<algorithm>using namespace std;const int DQ = 500000000;struct record{    string name, place;    int rd[4], total2, total4;    double money;    bool pro;    int no;};int cmp2(const record &a,const record& b){    if(a.total2!=b.total2)return a.total2<b.total2;    else return a.name<b.name;}int cmp4(const record &a,const record& b){    if(a.total4!=b.total4)return a.total4<b.total4;    else return a.name<b.name;}double prize_all, prize[70];vector<record> player;int str2num(const string &s){    int d=0;    for(string::const_iterator it=s.begin();it!=s.end();++it) d=d*10+(*it-'0');    return d;}inline void read_prize(){    cin>>prize_all;    for(int i=0;i!=70;++i){        cin>>prize[i];        prize[i]=prize[i]/100.0*prize_all;    }}inline void read_player(){    int num;    cin>>num;    player.resize(num);    string in;    getline(cin,in);    for(vector<record>::iterator it=player.begin();it!=player.end();++it){        getline(cin,in);        it->name=in.substr(0,20);        in=in.substr(20,in.size());        istringstream ss(in);        it->rd[0]=it->rd[1]=it->rd[2]=it->rd[3]=DQ,it->total2=it->total4=0;        for(int i=0;i!=4;++i){            ss>>in;            if(in=="DQ") break;            else it->rd[i]=str2num(in);        }        for(int i=0;i!=2;++i) it->total2+=it->rd[i];        for(int i=0;i!=4;++i) it->total4+=it->rd[i];        for(string::reverse_iterator sit=it->name.rbegin();sit!=it->name.rend();++sit) if(*sit!=' '){            if(*sit=='*') it->pro=false;            else it->pro=true;            break;        }    }}inline void select(){    int d=0,sum=0;    sort(player.begin(),player.end(),cmp2);    for(vector<record>::iterator it=player.begin();d!=70&&it!=player.end()&&(it->rd[0]!=DQ&&it->rd[1]!=DQ);++it){        ++sum,++d;        if(d==70){            int last=it->total2;            ++it;            while(it!=player.end()&&it->total2==last)++sum,++it;            break;        }    }    player.resize(sum);    sort(player.begin(),player.end(),cmp4);}vector<record>::iterator findnext(vector<record>::iterator it){    do{        ++it;    }while(it!=player.end()&&!(it->pro));    return it;}inline void getplace(){    int place=1;    for(vector<record>::iterator it=player.begin(),next;it!=player.end()&&(it->total4 < DQ);++it,++place){        ostringstream ss;        ss<<place;        it->place=ss.str();        next=it+1;        int d=it->pro?1:0;        while(next!=player.end()&&next->total4==it->total4){            next->place=ss.str();            if(next->pro) ++d;            ++next,++place;        }        if(d>1) while(it!=next){            if(!(it->money<0)) it->place+='T';            ++it;        }        it=next-1;    }}inline void getmoney(){    int place=0;    for(vector<record>::iterator it=player.begin();it!=player.end();++it) it->money=-1, it->no=-1;    for(vector<record>::iterator it=player.begin(),next;it!=player.end()&&(it->total4<DQ)&&place<70;++it) if(it->pro) next=findnext(it);    for(vector<record>::iterator it=player.begin(),next;it!=player.end()&&(it->total4<DQ)&&place<70;++it) if(it->pro){        next=findnext(it);        int d=1; double sum=prize[place];        it->no=place;        while(next!=player.end()&&next->total4==it->total4){            next=findnext(next);            ++d,++place;            next->no=it->no;            if(place<70) sum+=prize[place];        }        sum/=d;        while(it!=next){            if(it->pro) it->money=sum;            ++it;        }        it=next-1;        ++place;    }}inline void printans(){    cout<<"Player Name          Place     RD1  RD2  RD3  RD4  TOTAL     Money Won\n";    cout<<"-----------------------------------------------------------------------\n";    for(vector<record>::iterator it=player.begin();it!=player.end();++it){        cout<<it->name<<' ';        cout<<left<<setw(9)<<(it->total4>=DQ?" ":it->place);        for(int i=0;i!=4;++i){            if(it->rd[i]!=DQ) cout<<' '<<left<<setw(4)<<it->rd[i];            else cout<<' '<<left<<setw(4)<<' ';        }        if(it->total4>=DQ) cout<<' '<<"DQ";        else{            cout.precision(2);            cout.setf(ios::fixed);            if(it->pro && it->no < 70 && !(it->money<0)){                cout<<' '<<left<<setw(10)<<it->total4<<'$'<<right<<setw(9)<<it->money+1e-8;            }else   cout << ' ' << it->total4;        }        cout<<'\n';    }}int main(){    ios::sync_with_stdio(false);    int T;    cin>>T;    while(T--){        read_prize();        read_player();        select();        getmoney();        getplace();        printans();        if(T) cout<<endl;    }    return 0;}

0 0
原创粉丝点击