Topic:
The Codejamon game is on fire! Fans across the world is predicting and betting on which team would win the game.
A Gambling company are 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 don't have a 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 is the teams, with odds of 5:3 and 2:7 and you bet¥20 on the first team and¥10 on the S Econd team. If The first team wins, you'll lose your¥10 bet on the second team, but you'll receive your¥20 bet back, plus 3/5x A total of of¥32 at the end. If the second team wins, you'll lose your¥20 bet on the first team, but you'll receive your¥10 bet back, plus 7/2x Ten =, so you'll have a total of of¥45 at the end. Either, you'll have the more money than your 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 the them wins, you'll alwa Ys end up with more money than you bet. Can you figure out how many teams your can bet on?
Input:
The input starts with one line containing exactly one integer T, which is the number of the test cases. Each test case is starts with a line containing a integer n:the number of teams in the game. Then, N more lines follow. Each line was a pair of numbers in the form Ai:bi (that's, a number Ai, followed by a colon, then a number Bi, with no S paces 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 th E Maximum number of teams that you can bet on, under the conditions specified in the problem statement.
Note:
In the sample case #1, one optimal strategy are to bet 1.5 dollars on the first team and 1.5 dollars on the third team. If the first team wins, you'll get 1.5 + 1.5x (1.1/1) = 3.15 dollars back, and if the third team wins, you'll get 1.5 + (1.7/1.5) x1.5 = 3.2 dollars back. Both of these is higher than the total money and you bet (1.5 + 1.5 = 3 dollars). However, there is no-to-bet on all three teams and being guaranteed a profit.
Test instructions
There are n teams, you now bet on certain teams, ask for the number of teams to bet as much as possible, and as long as there is a team in the betting team win will be able to earn back, ask the most can bet how many teams.
Ideas:
If you want a team to win, you can earn back the original, that each team won at least to earn back. So you can use the cost to find the amount of money to bet. The amount of the bet is x, then sum = x* (1+b/a), and then the sum of the array is sorted, and exits when the sum is greater than or equal to the cost.
Ps:
This problem card long double, card to suspect thinking wrong, the last teammate Test instructions try a long double, unexpectedly!!!
A long double represents a larger range than a double and represents a greater precision. The speed of the corresponding calculation is also slow.
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>#include<map>#include<Set>#include<vector>using namespaceStd;typedefLong Longll;Const intMAXN = 1e5+Ten;Long Doublea[ the];intMain () {Ios::sync_with_stdio (false); intT,n,cnt=1; Charch; CIN>>T; while(t--) {memset (A,0,sizeof(a)); Long Doublesum =1.0, Aa,b; CIN>>N; for(inti =0; i<n; i++) {cin>>aa>>ch>>b; A[i]= sum/(1.0+b/AA); } sort (A,a+N); intAns =0; Long Doubleres =0.0; for(inti =0; i<n; i++) {res+=A[i]; if(res<sum) ans++; Else Break; } cout<<"Case #"<<cnt++<<": "<<ans<<Endl; } return 0;}/*Sample Input: 131:1.11:0.21.5:1.7 sample output: Case #1:2*/
View Code
Bet (The ACM-ICPC Asia china-final Contest thought problem)