Balls(UVALive 4554,dp)

Description

The classic Two Glass Balls brain-teaser is often posed as:

            “Given two identical glass spheres, you would like to determine the lowest floor in a 100-story

            building from which they will break when dropped. Assume the spheres are undamaged when

            dropped below this point. What is the strategy that will minimize the worst-case scenario for

            number of drops?”

      Suppose that we had only one ball. We’d have to drop from each floor from 1 to 100 in sequence, requiring 100 drops in the worst case.

      Now consider the case where we have two balls. Suppose we drop the first ball from floor n. If it breaks we’re in the case where we have one ball remaining and we need to drop from floors 1 to n − 1 in sequence, yielding n drops in the worst case (the first ball is dropped once, the second at most n − 1 times). However, if it does not break when dropped from floor n, we have reduced the problem to dropping from floors n + 1 to 100. In either case we must keep in mind that we’ve already used one drop. So the minimum number of drops, in the worst case, is the minimum over all n.         You will write a program to determine the minimum number of drops required, in the worst case, given B balls and an M-story building.

Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set consists of a single line containing three (3) decimal integer values: the problem number, followed by a space, followed by the number of balls B, (1 ≤ B ≤ 50), followed by a space and the number of floors in the building M, (1 ≤ M ≤ 1000).

Output

For each data set, generate one line of output with the following values: The data set number as a decimal integer, a space, and the minimum number of drops needed for the corresponding values of B and M.

Sample Input

4

1 2 10

2 2 100

3 2 300

4 25 900

Sample Output

1 4

2 14

3 24

4 10

题解:

从给定楼层内扔给定的玻璃球,问最少多少次可以找出正好要摔破的楼层。

很明显这是一个dp题,构造一个二位dp数组,第一层为楼层数,第二层为球数,dp值为找出的最优次数。

考虑dp[i][j](j即m)的情况分为两种:

(1)破。        这时应该考虑第k层下面的情况,用j-1个球,表示为dp[k-1][j-1]。

(2)不破。    这时应该考虑第k层上面的情况,用j个球,表示为dp[i-k][j]。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define maxn 320007
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1000000009
#define e  2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
#define fori(n) for(int i=0;i<n;i++)
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
using namespace std;

int dp[1111][1111];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int m,n,cnt;
        cin>>cnt>>m>>n;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                dp[i][j]=i;
        for(int i=1; i<=n; i++)
            for(int j=2; j<=m; j++)
            {
                for(int k=1; k<=i; k++)
                    dp[i][j]=min(dp[i][j],max(dp[i-k][j]+1,dp[k-1][j-1]+1));

            }
        cout<<cnt<<" "<<dp[n][m]<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值