Bet(2016-EC-Final-E题)

记录一道卡long double的毒瘤题。

记住以后WA了可以试一下long double

题目:

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 i-th 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 i-th 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 i-th 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.

Limits:

• 1 ≤ T ≤ 100.

• 1 ≤ N ≤ 100.

• 0 < Ai , Bi < 100.

• Both Ai and Bi have at most 3 digits after the decimal point.

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.

 

Sample Input

1

3

1:1.1

1:0.2

1.5:1.7

Sample Output

Case #1: 2

题意:给你N个比例,问你最多下注几个,使得任意一支队伍赢了都能赚回本钱。

题解:设本钱为x,对于每一个队伍,都可以计算出赚回本钱最少的下注量x*a/(a+b),然后将下注量排序,贪心取队伍就行,直到超过本钱。为了计算方便我们取x=1;注意这题卡long double;

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

int T,N,ans;
long double a,b;
long double p[105];
char ch;

int main()
{
	scanf("%d",&T);
	for (int cnt=1;cnt<=T;cnt++)
	{
		long double temp=0.0;
		scanf("%d",&N);
		ans=0;
		for (int i=1;i<=N;i++)
		{
			cin >> a >> ch >> b;
			p[i]=a/(a+b);
		}
		sort(p+1,p+1+N);
		for (int i=1;i<=N;i++)
		{
			temp+=p[i];
			if (temp<1.0)
				ans++;
			else
				break;
		}
		printf("Case #%d: %d\n",cnt,ans);
	}
	return 0;
}

 

转载于:https://www.cnblogs.com/Radium1209/p/10415335.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值