题意: 有n个球队比赛,你可以赌哪个队获胜.如果你下注的队赢了,你将获得c + ai / bi * ci的钱,如果输了,那么你将输ci (ci为你下注的钱数).问, 在确保你一定能得到比原来钱数多的前提下,你最多可以赌多少个队获胜(你下注的队伍至少有一支会赢得比赛).
分析: 假设原来总共有1元钱,在第i个足球队下注ci元钱,那么如果要最后获得的钱数大于1,则必有ci + ai / bi * ci > 1 => ci > ai / (ai + bi) ,显然在下注第i个足球队后,你剩余的钱数为 1 - ci.问题就转变成了在满足 ∑(ai / (ai + bi)) < 1 下, 赌最多的队获胜. 将ai / (ai + bi)排序后按从小到大取即可.
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
int v = 1;
BigInteger[] a = new BigInteger[120];
BigInteger[] b = new BigInteger[120];
while(t-- > 0) {
int n = cin.nextInt();
for(int i = 0; i < n; i++) {
String str = cin.next();
String[] st = str.split(":");
double x = Double.parseDouble(st[0]) * 1000;
double y = Double.parseDouble(st[1]) * 1000;
a[i] = new BigInteger((long)x + "");
b[i] = new BigInteger((long)y + "");
}
BigInteger tmp = new BigInteger("0");
BigInteger tem = new BigInteger("0");
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(a[i].multiply(b[j].add(a[j])).compareTo(a[j].multiply(a[i].add(b[i]))) > 0) {
tmp = b[i];
tem = a[i];
a[i] = a[j];
b[i] = b[j];
a[j] = tem;
b[j] = tmp;
}
}
}
int ans = 1;
BigInteger sum = a[0].add(b[0]), rr = a[0];
for(int i = 1; i < n; i++) {
BigInteger sum1 = sum.multiply(a[i].add(b[i]));
rr = rr.multiply(a[i].add(b[i]));
rr = rr.add(sum.multiply(a[i]));
sum = sum1;
//System.out.println(sum + " " + rr);
if(sum.compareTo(rr) <= 0) break;
ans++;
}
System.out.println("Case #" + v++ + ": " + ans);
}
cin.close();
}
}