本文主要是介绍Dropping water balloons,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
It’s frosh week, and this year your friends have decided that they would initiate the new computer science students by dropping water balloons on them. They’ve filled up a large crate of identical water balloons, ready for the event. But as fate would have it, the balloons turned out to be rather tough, and can be dropped from a height of several stories without bursting!
So your friends have sought you out for help. They plan to drop the balloons from a tall building on campus, but would like to spend as little effort as possible hauling their balloons up the stairs, so they would like to know the lowest floor from which they can drop the balloons so that they do burst.
You know the building has n floors, and your friends have given you k identical balloons which you may use (and break) during your trials to find their answer. Since you are also lazy, you would like to determine the minimum number of trials you must conduct in order to determine with absolute certainty the lowest floor from which you can drop a balloon so that it bursts (or in the worst case, that the balloons will not burst even when dropped from the top floor). A trial consists of dropping a balloon from a certain floor. If a balloon fails to burst for a trial, you can fetch it and use it again for another trial.
Input
The input consists of a number of test cases, one case per line. The data for one test case consists of two numbers k and n, 1 ≤ k ≤ 100 and a positive n that fits into a 64 bit integer (yes, it’s a very tall building). The last case has k = 0 and should not be processed.
Output
For each case of the input, print one line of output giving the minimum number of trials needed to solve the problem. If more than 63 trials are needed then print ‘More than 63 trials needed.’ instead of the number.
Sample Input
2 100
10 786599
4 786599
60 1844674407370955161
63 9223372036854775807
0 0
Sample Output
14
21
More than 63 trials needed.
61
63
思路:
动态规划。
转移方程确定了一切就迎刃而解了。
设d(i,j)是i个球j次实验下所能测试的最高层数。
设测试层数为k。
- 若在k层气球破了,前k-1层必须能用i-1个球做j-1个实验。则k=d(i-1,j-1)+1最优。
- 若没破,则之后的楼层必须得用i个球全做。则d(i,j)-k=d(i,j-1)。
则d(i,j)=k+d(i,j-1)=d(i-1,j-1)+d(i,j-1)+1。即为转换方程。
之后就是根据条件确定最终答案了。
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
typedef unsigned long long ll;
const int K=100;
const int A=63;
ll d[K+1][A+1];
int main()
{ //打表 for(int i=1;i<=K;i++)for(int j=1;j<=A;j++){d[i][j]=d[i-1][j-1]+1+d[i][j-1];//printf("%lld\n",d[i][j]);}int k;ll n;while(scanf("%d %lld",&k,&n)!=EOF&&k){int ans=-1;for(int i=1;i<=A;i++){if(d[k][i]>=n){ans=i;break;}}if(ans<0)printf("More than 63 trials needed.\n");else printf("%d\n",ans);}return 0;
}
这篇关于Dropping water balloons的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!