poj-3783-Balls dp/线性dp

Balls
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 792 Accepted: 513

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

Source


紫书题。

本该是道简单dp题, 要反思总结。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

#define ll long long
#define rep(x, y, z) for(int x=y;x<z;x++)
const int maxn=1e4+7;
int T;
int kase;
int n, m;
ll dp[53][maxn];
void init(){
    //memset(dp, 0, sizeof(dp));
    rep(i, 1, 52){
        rep(j, 1, maxn){
            if(dp[i-1][j-1]+1+dp[i][j-1]>2000) dp[i][j]=2000;
            else dp[i][j]=dp[i-1][j-1]+dp[i][j-1]+1;
        }
    }
}
void solve(){
    
    int ans=int(lower_bound(dp[n], dp[n]+maxn, m)-dp[n]);
    printf("%d %d\n", kase, ans);
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    scanf("%d", &T);
    init();
    while(T--){
        
        scanf("%d%d%d", &kase, &n, &m);
        solve();
    }
    
    return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值