Balls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 907   Accepted: 598

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

------------------------------------------------
经典问题
----------------------------
f[i][j]表示i层楼j个蛋的最坏情况最优解
两种可能,碎或不碎f[i][j]=min(f[i][j],max(f[i-1][k-1],f[i][j-k])+1)
初始化INF,f[i][0]=0,f[1][j]=j 不是j-1
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N=,B=,INF=1e9;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int p,b,m,ans=INF,no;
int f[B][N];
void dp(){
for(int i=;i<=b;i++) for(int j=;j<=m;j++) f[i][j]=INF,f[][j]=j;
for(int i=;i<=b;i++)
for(int j=;j<=m;j++)
for(int k=;k<=j;k++)
f[i][j]=min(f[i][j],max(f[i-][k-],f[i][j-k])+);
}
int main(int argc, const char * argv[]) {
p=read();
for(int i=;i<=p;i++){
no=read();b=read();m=read();
dp();
ans=f[b][m];
printf("%d %d\n",no,ans);
}
return ;
}

POJ3783Balls[DP 最坏情况最优解]的更多相关文章

  1. 7.12 NOI模拟赛 探险队 期望 博弈 dp 最坏情况下最优策略 可并堆

    LINK:探险队 非常难的题目 考试的时候爆零了 完全没有想到到到底怎么做 (当时去刚一道数论题了. 首先考虑清楚一件事情 就是当前是知道整张地图的样子 但是不清楚到底哪条边断了. 所以我们要做的其实 ...

  2. k个鸡蛋从N楼层摔,如果确定刚好摔碎的那个楼层,最坏情况下最少要试验x次?

    题目 k个鸡蛋从N楼层摔,如果确定刚好摔碎的那个楼层,最坏情况下最少要试验x次? 换个说法: k个鸡蛋试验x次最多可以检测N层楼.计算出N? 逆向思维和数学公式解. 分析 定义N(k,x) 如果第k个 ...

  3. 一种最坏情况线性运行时间的选择算法 - The missing worst-case linear-time Select algorithm in CLRS.

    一种最坏情况线性运行时间的选择算法 - The missing worst-case linear-time Select algorithm in CLRS. 选择算法也就是求一个无序数组中第K大( ...

  4. 区间DP(区间最优解)题目讲解总结

    1:给出一个括号字符串,问这个字符串中符合规则的最长子串的长度. [分析]区间DP要覆盖整个区间,那么要求所有情况的并集. 先想出状态方程: dp[i][j]:i ~ j区间内最大匹配数目 输出:dp ...

  5. LCS nlog(n) 但最坏情况还是比较悲剧 转载的文章;

    最长公共子序列问题: 给定2个字符串,求其最长公共子串.如abcde和dbada的最长公共字串为bd. 动态规划:dp[i][j]表示A串前i个和B串前j个的最长公共子串的长度. 则 若A[i] == ...

  6. UVA 10859 Placing Lamppost 树形DP+二目标最优解的求解方案

    题意:给定一个无向,无环,无多重边,要求找出最少的若干点,使得,每条边之中至少有一个点上有街灯.在满足上述条件的时候将还需要满足让两个点被选择的边的数量尽量多. 题解: 对于如何求解最小的节点数目这点 ...

  7. CF Gym 100187A Potion of Immortality (思路,最坏情况的最小损失)

    根据兔子试药情况可以缩小范围,如果死了,不在试过的药里面,如果活着,在试过的药里. 最糟的情况: 两个原则 1.能确定魔药所在的范围的尽量大,2.死得兔子尽量多. 如果当前不知道情况的药n为k的二倍以 ...

  8. csacademy Round #36(模拟+最坏情况)

    传送门 题意 给出n种袜子,每种袜子个数a[i],两只相同种类袜子配成一对,询问至少拿出多少只袜子能确保配出k对袜子 分析 In order to find out the minimum numbe ...

  9. 一道算法问题:一幢 200 层的大楼,给你两个鸡蛋. 如果在第 n 层扔下鸡蛋,鸡蛋不碎,那么从前 n-1 层扔鸡蛋都不碎. 这两只鸡蛋一模一样,不碎的话可以扔无数次. 已知鸡蛋在0层扔不会碎. 提出一个策略, 要保证能测出鸡蛋恰好会碎的楼层, 并使此策略在最坏情况下所扔次数最少.

    今晚要参加网易的笔试,所以一直在刷题,刷到这个题的时候觉得自己的思路很模糊,就去网上百度了一下,找到一个大神给的解决方案: 如下: (http://ppwwyyxx.com/2013/Problem- ...

随机推荐

  1. 异常之Tomcat7.0服务器无法发布项目

    今天突然就不能发布tomcat 7.0服务器了,并弹出对话框,报出如下错误: Cannot acquire J2EEFlexProjDeployable object for module test ...

  2. jQuery原型方法first,last,eq,slice源码分析

    这4个方法中前3个方法很常用大家都见过,但是slice方法可能会以为是数组方法,其实slice也是jQuery的一个原型方法,只不过是底层方法是为其他方法服务的(更具体点是为eq方法服务的),首先还是 ...

  3. jQuery 重要的知识点归纳

    jQuery 对象 jQuery 对象就是通过 jQuery 包装 DOM 对象后产生的对象. jQuery 对象是 jQuery 独有的. 只有 jQuery 对象才能使用 jQuery 的方法,在 ...

  4. unbuntu apahce 2 设置 多域名

    1.找到apache2 的设置路径 默认的apache的路径为/etc/apache2/ 2. 修改httpd.conf 本文192.168.0.1 为自己的服务器的ip,下面一样的意思 Server ...

  5. 【代码笔记】iOS-按钮带下划线的写法

    一,效果图. 二,工程图. 三,代码. RootViewController.h RootViewController.m - (void)viewDidLoad { [super viewDidLo ...

  6. [Android] android .keystore文件转x509pem工具

    .keystore是android的签名文件,最近在做联通联运的时候,发现他们需要上传x509pem格式的文件来签名所以就研究了一下如何转化 方法一:使用openssl的方法 http://blog. ...

  7. TFS2012 自动生成与部署

    思路: 每日构建,自动生成,然后从TFS提交日志中提取版本修订说明,调用打包脚本混淆并生成安装包(系统自带的太锉),最后将相关文件复制到指定网站供浏览下载.自动向测试网站发布,自动生成数据库并初始化. ...

  8. 源码编译安装screen

    OS:Amazon Linux AMI 2015.09.2 (HVM) #sudo su #wget http://ftp.gnu.org/gnu/screen/screen-4.3.1.tar.gz ...

  9. 基于Tomcat + JNDI + ActiveMQ实现JMS的点对点消息传送

    前言 写了一个简单的JMS例子,之所以使用JNDI 是出于通用性考虑,该例子使用JMS规范提供的通用接口,没有使用具体JMS提供者的接口,这样可以保证我们编写的程序适用于任何一种JMS实现(Activ ...

  10. Fourth glance in Go

    在Go里面的结构体和方法是很有意思的内容.它即和OO有点像,但又不完全一样.为什么这么说呢?当你把结构体映射为OO里面的类的时候,你给结构体添加方法,你就会发现其实方法与结构体并没有严格意义上的关系. ...