【Gym-101194E】Problem E. Bet - The 2016 ACM-ICPC Asia China-Final Contest

Bet 题目链接

pdf链接

题目描述

Input file: Standard Input
Output file: Standard Ouptut
Time limit: 1 second


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 B i A i \frac{Bi}{Ai} times your bet on that team.
For example, suppose that there are two teams, with odds of 5 : 3 5:3 and 2 : 7 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 \frac{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 \frac{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.

Sample Input

1
3
1:1.1
1:0.2
1.5:1.7

Sample Output

Case #1: 2

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.

题意

有一个赌徒,要去赌钱。
赌钱规则如下:
  1. 有 N 个队的比赛。
  2. 每个队都有一个属性 Ai:Bi。
  3. 你可以下注任意多个队赢。
  4. 如果你下注的队赢了,你就可以拿回你下注的钱,并加上 你下注的钱 * (Bi/Ai)。(如果你给第 i 支队伍下注 X 元,并且这支队伍赢了,你可以获得 X + X * (Bi/Ai) 元)。
  5. 没有赢的队伍的下注的钱,全部没收。
题目给 N 个队伍,问你最多可以选多少支队伍出来,至少存在一种方法,使得不管这些队伍里哪支队伍赢了,你都有钱赚。
例如题目举的栗子,第一支队伍赔率为 5:3,第二支队伍为 7:2,你只要给第一支队伍投 20 元,给第二支队伍投 10 元,不管这两支队伍中哪一个赢了,你都有钱赚。

题解

我们假设有 n n 支队伍,总存在至少一种下注方法,使得不管结果如何,都可以赚钱。
我们不妨把 B A \frac{B}{A} 叫做赔率。

n = 1 n = 1 时,不管怎么投注, 1 1 支队伍必胜。
n = 2 n = 2 时,

队伍 赔率 下注金额
1 a x
2 b y

如果第一队赢,若要赚钱,则有式子: x + a x &gt; x + y x + ax &gt; x + y (获得的钱要大于投注的钱)
如果第二队赢,若要赚钱,则有式子: y + b y &gt; x + y y + by &gt; x + y
如果要让不管哪一队赢都可以赚钱,显然要让两式联立有解: { x + a x &gt; x + y y + b y &gt; x + y \begin{cases} x + ax &gt; x + y\\ y + by &gt; x + y\\ \end{cases}
我们可以对这个式子做一下变换, { x ( 1 + a ) &gt; x + y y ( 1 + b ) &gt; x + y \begin{cases} x(1 + a) &gt; x + y\\ y(1 + b) &gt; x + y\\ \end{cases} 然后变成: { 1 + a &gt; x + y x 1 + b &gt; x + y y \begin{cases} 1 + a &gt; \frac{x + y}{x}\\ 1 + b &gt; \frac{x + y}{y}\\ \end{cases}
根据题目,可知赔率 a a b b 都是大于 0 0 的,所以式子的左边和右边都是大于 0 0 的。
因此我们可以取一下倒数,变成: { 1 1 + a &lt; x x + y 1 1 + b &lt; y x + y \begin{cases} \frac{1}{1 + a} &lt; \frac{x}{x + y}\\ \frac{1}{1 + b} &lt; \frac{y}{x + y}\\ \end{cases}
至此, n = 2 n = 2 的结果已经很明显了,两式右边相加为 1 1 ,即 x x + y + y x + y = 1 \frac{x}{x + y} + \frac{y}{x + y} = 1
由于 x x y y 是下注金额,是可以任意取值的,所以两式等号右边可以任意取成相加为 1 1 的实数。
因此只要两式的左边 1 1 + a \frac{1}{1 + a} 1 1 + b \frac{1}{1 + b} 只要相加小于 1 1 ,右边的 x x + y \frac{x}{x + y} y x + y = 1 \frac{y}{x + y} = 1 就必然存在至少一个解 ( x , y ) (x, y) 使得 { 1 1 + a &lt; x x + y 1 1 + b &lt; y x + y \begin{cases} \frac{1}{1 + a} &lt; \frac{x}{x + y}\\ \frac{1}{1 + b} &lt; \frac{y}{x + y}\\ \end{cases} 成立。


我们可以继续观察 n = 3 n = 3 的情况。

队伍 赔率 下注金额
1 a x
2 b y
3 c z

此时根据 n = 2 n = 2 一样的推导操作,可以得到: { 1 1 + a &lt; x x + y + z 1 1 + b &lt; y x + y + z 1 1 + c &lt; z x + y + z \begin{cases} \frac{1}{1 + a} &lt; \frac{x}{x + y + z}\\ \frac{1}{1 + b} &lt; \frac{y}{x + y + z}\\ \frac{1}{1 + c} &lt; \frac{z}{x + y + z}\\ \end{cases}
因此,只要 1 1 + a \frac{1}{1 + a} 1 1 + b \frac{1}{1 + b} 1 1 + c \frac{1}{1 + c} 的和小于 1 1 , 就必然至少存在一个解 ( x , y , z ) (x, y, z) 使得上面式子成立。


因此当 n n 为任意实数时,我们设赔率为 p i p_i ,投注为 t i t_i
可得:
{ 1 1 + p 1 &lt; t 1 t 1 + t 2 + + t n 1 1 + p 2 &lt; t 2 t 1 + t 2 + + t n 1 1 + p n &lt; t n t 1 + t 2 + + t n \begin{cases} \frac{1}{1 + p_1} &lt; \frac{t_1}{t_1 + t_2 +\cdots + t_n}\\ \frac{1}{1 + p_2} &lt; \frac{t_2}{t_1 + t_2 +\cdots + t_n}\\ \quad\quad\quad\vdots\\ \frac{1}{1 + p_n} &lt; \frac{t_n}{t_1 + t_2 +\cdots + t_n}\\ \end{cases}
只要式子左边加起来小于 1 1 , 则一定存在至少一个解 ( t 1 , t 2 , &ThinSpace; , t n ) (t_1,t_2,\cdots,t_n) 使得等式成立。


因此,对于这道题,我们只需要找到 1 1 + ( B / A ) \frac{1}{1 + (B/A)} 加起来小于 1 1 的最大个数就行了。
我们可以考虑用贪心的方法,先把 1 1 + ( B / A ) \frac{1}{1 + (B/A)} 从小到大排序,直到和大于 1 1

但是对于这道题而言,double 的精度不符合要求,需要用到 long double

并且输入数据之间有 ":",用 printf 会出现未知错误,windows 上无法体现,judge 机器会返回 WA。

代码

#include<algorithm>
#include<iostream>
using namespace std;
long double a[1000], x, y;

int main(){
	int T, n, ca = 0;
	char c;
	cin >> T;
	while (T--) {
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> x >> c >> y;
			a[i] = 1.0/(long double)(y/x + 1);
		}
		sort(a, a + n);
		long double sum = 0;
		int i;
		for (i = 0; i < n; i++) {
			sum += a[i];
			if (sum >= 1) {
				break;
			}
		}
		printf("Case #%d: %d\n", ++ca, i);
	}
}
发布了79 篇原创文章 · 获赞 56 · 访问量 50万+

猜你喜欢

转载自blog.csdn.net/qq_40861916/article/details/84403731