【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 $\frac{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 $\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 $\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$ 支队伍,总存在至少一种下注方法,使得不管结果如何,都可以赚钱。
我们不妨把 $\frac{B}{A}$ 叫做赔率。

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


我们可以继续观察 $n = 3$ 的情况。
|队伍|赔率|下注金额|
|:—-:|:—-:|:—-:|
|1|a|x|
|2|b|y|
|3|c|z|
此时根据 $n = 2$ 一样的推导操作,可以得到: $\begin{cases}
\frac{1}{1 + a} < \frac{x}{x + y + z}\\
\frac{1}{1 + b} < \frac{y}{x + y + z}\\
\frac{1}{1 + c} < \frac{z}{x + y + z}\\
\end{cases}$
因此,只要 $\frac{1}{1 + a}$、$\frac{1}{1 + b}$ 和 $\frac{1}{1 + c}$ 的和小于 $1$, 就必然至少存在一个解 $(x, y, z)$ 使得上面式子成立。


因此当 $n$ 为任意实数时,我们设赔率为 $p_i$,投注为 $t_i$。
可得:

只要式子左边加起来小于 $1$, 则一定存在至少一个解 $(t_1,t_2,\cdots,t_n)$ 使得等式成立。


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

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

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

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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);
}
}