Dropping water balloons UVA - 10934 (奇怪的dp,思维题)

24 篇文章 0 订阅

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

大致题意:给你k个相同的水球,在一个n层高的楼上做实验,你想知道最低从第几层楼上扔下水球会破掉,破掉的水球不能再次用,没破掉的可以再次拿来用,在最糟糕的情况下,问你最少需要扔几次,如果超过63次,则输出More than 63 trials needed.

思路:emmmm很难想的很奇怪的dp,首先,假设dp[i][j]表示有i个球,扔j次所能确定的最多的楼层数,
那么问题就变成了现在已知有k个球,n层楼,然后让你找到一个最小的j使得dp[k][j]>=n。
状态转移方程:dp[i][j]=dp[i-1][j-1]+dp[i][j-1]+1;

代码如下

#include <cstdio>  
#include <cstring>  
#include <algorithm> 
#include <iostream> 
using namespace std;  
#define LL long long
LL dp[105][64];
void init()
{
    dp[0][0]=0;
    for(int i=1;i<=100;i++)
    for(int j=1;j<=63;j++)
    dp[i][j]=dp[i][j-1]+1+dp[i-1][j-1];
}
int main()
{
    int k;
    LL n;
    init();
    while(cin>>k>>n)
    {
        if(!k)
        break;

        int f=0;
        for(int j=1;j<=63;j++)
        if(dp[k][j]>=n)
        {
            cout<<j<<endl;
            f=1;
            break;
        }
        if(!f)
        cout<<"More than 63 trials needed.\n";
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值