算典05_例题_10_UVA-207

PGA Tour Prize Money

题意

    为PGA(美国职业高尔夫球协会)巡回赛计算奖金。巡回赛分为4轮,其中所有选手都能打前两轮(除非中途取消资格),得分相加(越少越好),前70名(包括并列)晋级(make the cut)。所有晋级选手再打两轮,前70名(包括并列)有奖金。组委会事
先会公布每个名次能拿的奖金比例。例如,若冠军比例是18%,总奖金是$1000000,则冠军奖金是$180000。
    输入保证冠军不会并列。如果第k名有n人并列,则第k~n+k-1名的奖金比例相加后平
均分给这n个人。奖金四舍五入到美分。所有业余选手不得奖金。例如,若业余选手得了第3
名,则第4名会拿第3名的奖金比例。如果没取消资格的非业余选手小于70名,则剩下的奖金
就不发了。

题解

细节题
直接找的网上的代码,感觉难度不是很大,但是略为烦琐

#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 << "-----------------------------------------------------------------------" << endl;
    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);
//    freopen("in.txt", "r", stdin);
//    freopen("o.txt", "w", stdout);

    int T;
    cin >> T;
    while(T--) {
        read_prize();
        read_player();

        select();
        getmoney();
        getplace();

        printans();
        if(T) cout << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值