1. 程式人生 > >Bet(The 2016 ACM-ICPC Asia China-Final Contest 思路題)

Bet(The 2016 ACM-ICPC Asia China-Final Contest 思路題)

suppose spec case cpc mon 輸入 under strong ces

題目:

  The Codejamon game is on fire! Fans across the world are predicting and betting on which team will win the game.

  A gambling company is providing betting odds for all teams; the odds for the ith team is Ai :Bi . For each team, you can bet any positive amount of money, and you do not have to bet the same amount on each team. If the ith

team wins, you get your bet on that team back, plus Bi / Ai times your bet on that team.

  For example, suppose that there are two teams, with odds of 5:3 and 2:7 and you bet ¥20 on the first team and ¥10 on the second team. If the first team wins, you will lose your ¥10 bet on the second team, but you will receive your ¥20 bet back, plus 3/5 × 20 = 12, so you will have a total of ¥32 at the end. If the second team wins, you will lose your ¥20 bet on the first team, but you will receive your ¥10 bet back, plus 7/2 × 10 = 35, so you will have a total of ¥45 at the end. Either way, you will have more money than you bet (¥20+¥10=¥30).

  As a greedy fan, you want to bet on as many teams as possible to make sure that as long as one of them wins, you will always end up with more money than you bet. Can you figure out how many teams you can bet on?

Input:

  The input starts with one line containing exactly one integer T, which is the number of test cases. Each test case starts with one line containing an integer N: the number of teams in the game. Then, N more lines follow. Each line is a pair of numbers in the form Ai

:Bi (that is, a number Ai, followed by a colon, then a number Bi , with no spaces in between), indicating the odds for the ith team.

Output:

  For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the maximum number of teams that you can bet on, under the conditions specified in the problem statement.

Note:

  In sample case #1, one optimal strategy is to bet 1.5 dollars on the first team and 1.5 dollars on the third team. If the first team wins, you will get 1.5 + 1.5 × (1.1/1) = 3.15 dollars back, and if the third team wins, you will get 1.5 + (1.7/1.5) × 1.5 = 3.2 dollars back. Both of these are higher than the total money that you bet (1.5 + 1.5 = 3 dollars). However, there is no way to bet on all three teams and be guaranteed a profit.

題意:

  有n個隊伍,你現在對某些隊伍下註,要求下註的隊伍的個數盡量多,而且只要下註的隊伍中有一支隊伍獲勝就能賺回本來,問最多可以下註多少支隊伍。

思路:

  要想一支隊伍贏就能賺回本來,那每支隊伍贏了至少要賺回本來。所以可以用本錢來求下註的錢數。設下註的錢數為x,則sum = x*(1+b/a);,之後對數組排序求和,當和大於等於本錢時退出。

PS:

  這個題卡long double,卡到懷疑思路錯了,最後隊友題意試一下long double,竟然過了!!!

  long double 要比double表示的範圍大,表示的精度也更大。相應的計算的時候速度也就慢了點。

技術分享圖片
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
long double a[110];

int main()
{
    ios::sync_with_stdio(false);
    int T,n,cnt=1;
    char ch;
    cin>>T;
    while(T--)
    {
        memset(a,0,sizeof(a));
        long double sum = 1.0,aa,b;
        cin>>n;
        for(int i = 0; i<n; i++)
        {
            cin>>aa>>ch>>b;
            a[i] = sum/(1.0+b/aa);
        }
        sort(a,a+n);
        int ans = 0;
        long double res = 0.0;
        for(int i = 0; i<n; i++)
        {
            res += a[i];
            if(res<sum)
                ans++;
            else
                break;
        }
        cout<<"Case #"<<cnt++<<": "<<ans<<endl;
    }
    return 0;
}

/*
樣例輸入:
1
3
1:1.1
1:0.2
1.5:1.7
樣例輸出:
Case #1: 2
*/
View Code

Bet(The 2016 ACM-ICPC Asia China-Final Contest 思路題)