[ICPC2016上海E] Bet

[ICPC2016上海E] Bet - 贪心

Description

给你每个队的赔率,你可以任意分配金额,问最多投多少个队使得只要有一个队赢了,就能赚钱。

Solution

预处理投每个队至少多少钱使得如果该队赢刚好回本,然后贪心选择即可,恶心的地方在于卡精度

#include <bits/stdc++.h>
using namespace std;

#define int long long

#define double long double

const int N = 1e2 + 5;
const double eps = 1e-36;

int caseid = 0;
int n;
double p[N];

void solve()
{
    ++caseid;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        double a, b;
        string str;
        cin >> str;
        for (int i = 0; i < str.length(); i++)
            if (str[i] == ':')
                str[i] = ' ';
        stringstream ss(str);
        ss >> a >> b;
        p[i] = a / (a + b);
    }
    cout << "Case #" << caseid << ": ";
    if (n == 1)
    {
        cout << 1 << endl;
        return;
    }
    sort(p + 1, p + n + 1);
    double res = 1;
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        double cost = p[i];
        if (res - cost > eps)
        {
            res -= cost;
            ans++;
        }
        else
            break;
    }
    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)
        solve();
}
原文地址:https://www.cnblogs.com/mollnn/p/14667829.html