dp zoj1503 One Person The Price is Right
【关键字】
动态规划
【摘要】
价格竞猜
【正文】
1.题目描述
One Person "The Price is Right"
--------------------------------------------------------------------------------
Time Limit: 1 Second Memory Limit: 32768 KB
--------------------------------------------------------------------------------
In the game show "The Price isRight", a number of players (typically 4) compete to get on stage byguessing the price of an item. The winner is the person whose guess is theclosest one not exceeding the actual price. Because of the popularity of theone-person game show "Who Wants to be a Millionaire", the AmericanContest Management (ACM) would like to introduce a one-person version of the"The Price is Right". In this version, each contestant is allowed G(1 <= G <= 30) guesses and L (0 <= L <= 30) lifelines. The contestantmakes a number of guesses for the actual price. After each guess, thecontestant is told whether it is correct, too low, or too high. If the guess iscorrect, the contestant wins. Otherwise, he uses up a guess. Additionally, ifhis guess is too high, a lifeline is also lost. The contestant loses when allhis guesses are used up or if his guess is too high and he has no lifelinesleft. All prices are positive integers.
It turns out that for a particular pair ofvalues for G and L, it is possible to obtain a guessing strategy such that ifthe price is between 1 and N (inclusive) for some N, then the player canguarantee a win. The ACM does not want every contestant to win, so it mustensure that the actual price exceeds N. At the same time, it does not want thegame to be too diffcult or there will not be enough winners to attractaudience. Thus, it wishes to adjust the values of G and L depending on theactual price. To help them decide the correct values of G and L, the ACM hasasked you to solve the following problem. Given G and L, what is the largestvalue of N such that there is a strategy to win as long as the price is between1 and N (inclusive)?
Input
The input consists of a number of cases.Each case is specified by one line containing two integers G and L, separatedby one space. The end of input is specified by a line in which G = L = 0.
Output
For each case, print a line of the form:
Case c: N
where c is the case number (starting from1) and N is the number computed.
Sample Input
3 0
3 1
10 5
7 7
0 0
Sample Output
Case 1: 3
Case 2: 6
Case 3: 847
Case 4: 127
2.算法分析
f[g,l]:g次猜的机会 ,l条生命线时可以猜的次数
递推函数:f(g,l)=f(g-1,l-1)+1+f(g-1,l)
初始化:f(i,0)=i (0<=i<=g)
f(0,i)=0 (0<=i<=l)
递推函数含义:
1、任意猜一个数m;
2、if m>price 在m前还有f(g-1,l-1)个值能猜;
3、if m<price 在m后还有f(g-1,l)个值能猜;
4、在加上已猜过m,所以一共有f(g-1,l-1)+1+f(g-1,l)个值能猜。
3.源码
#include <cstdio>
#include <cstring>
#define MAX 35
int f[MAX][MAX];
int g,l;
int main()
{
int cas=0;
while (scanf("%d %d",&g,&l),!((g==0)&&(l==0)))
{
memset(f,0,sizeof(f));
for(int i=0;i<=g;i++)
f[i][0]=i;
for(int i=1;i<=g;i++)
for(int j=1;j<=l;j++)
f[i][j]=f[i-1][j-1]+f[i-1][j]+1;
int ans=-1;
for(int i=0;i<=g;i++)
for(int j=0;j<=l;j++)
if (f[i][j]>ans)
ans=f[i][j];
cas++;
printf("Case %d: %d\n",cas,ans);
}
return 0;
}