1. 程式人生 > >2016 ACM-ICPC Asia China-Final E-bet

2016 ACM-ICPC Asia China-Final E-bet

Description

The Codejamon game is on re! 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 specied in the problem statement.

Examples

Input

1
3
1:1.1
1:0.2
1.5:1.7
  • 1
  • 2
  • 3
  • 4
  • 5

Output

Case #1: 2
  • 1

Problem description

有n個隊伍參加比賽,給你賠率比,輸出你能投入錢的最大隊伍的個數。在所有你投入的隊伍中,要保證只要任何一支隊伍贏,所獲得的金錢大於你所投入的總金錢。(各個隊伍之間獨立)

Solution

>設pi為投入第i個隊伍的金錢佔總金錢的比例>>設pi為投入第i個隊伍的金錢佔總金錢的比例>

>則pi∗(1+Bi/Ai)>1>>則pi∗(1+Bi/Ai)>1>

>即pi>Ai/(Ai+Bi)>>即pi>Ai/(Ai+Bi)>

則可按照

>Ai/(Ai+Bi)>>Ai/(Ai+Bi)>

從小到大排序,只要總和小於1,則從小到大一直選擇。
感覺牽扯到的問題有一個高精度除法那裡,可能除法的精度會出問題,如果不用 long double的話
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 100005
long double a[maxn];
const int esp=1000;
using namespace std;
int main()
{
    int t;
    cin>>t;
    for(int ca=1;ca<=t;ca++)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            long double A,B;
            char c;
            cin >>A>>c>>B;
            int t1=(A*esp+0.01);
            int t2=(B*esp+0.01);
            a[i]=1.0*A/(long double)(A+B);
        }
        sort(a+1,a+n+1);
        long double sum=0;
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(sum+a[i]>=1)
                break;
            ans++;
            sum+=a[i];
        }
        cout<<"Case #"<<ca<<": ";
        cout<<ans<<endl;
    }
    return 0;
}