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

题目大意:
有一些鸡蛋,我们现在想知道这些鸡蛋的硬度。然后现在有一座很高很高的大楼里,我们现在要在这座大楼上测试鸡蛋的硬度。每个鸡蛋的硬度相同,鸡蛋的硬度定义为:如果鸡蛋从第 m
层上掉下来没有破裂,而从第 m+1 层上掉下来就破裂了,那么这个鸡蛋的硬度就是 m 。某个鸡蛋如果在实验中破裂了就永远的损失了。我们现在有 n

个鸡蛋。那么在最坏情况下我们最少需要做多少次实验呢?

输入数据:是 T 组数据,然后第一个数 是标号 op
,然后输入两个整数 M,和 N,分别表示有 M 个鸡蛋和 N层楼。
输出数据:标号 op , 和最坏情况下我们最少需要做多少次实验 ans

解题思路:
这是一个比较经典的 DP
问题,又叫做 “扔鸡蛋问题”,假设 dp[n,m] 表示 n 层楼、m 个鸡蛋时找到摔鸡蛋不碎的最少判断次数。则一个鸡蛋从第 i 层扔下,如果碎了,还剩 m−1 个鸡蛋,为确定下面楼层中的安全位置,还需要dp[i−1,m−1] 次(子问题);不碎的话,上面还有 n−i 层,还需要 dp[n−i,m]次(子问题,实体 n 层楼的上 n−i 层需要的最少判断次数和实体 n−i 层楼需要的最少判断次数其实是一样的)。

 #include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
const int Inf=0x3f3f3f3f;
int m,n,k,p;
int str[];
int ans[];
int dp[][];////dp[i][j]:表示在 i 层楼 还有 j 个鸡蛋的最小判断次数
int di[][]={{-,},{,},{,-},{,}};
map<ll,ll>::iterator it;
void solve(int p,int k)
{
memset(dp,,sizeof(dp));
for(int i=;i<=k;i++)
{
dp[i][]=i;//只有一个鸡蛋的情况
}
for(int i=;i<=p;i++)
{
dp[][i]=;//只有一层楼的情况
}
for(int i=;i<=k;i++)
for(int j=;j<=p;j++)
{
dp[i][j]=Inf;
for(int t=;t<=i;t++)
{
dp[i][j]=min(dp[i][j],max(dp[t-][j-],dp[i-t][j])+);
}
}
cout<<n<<" "<<dp[k][p]<<endl;
}
int main()
{
cin>>m;
while(m--)
{
cin>>n>>p>>k;
solve(p,k);
}
}

Balls(poj 3783)的更多相关文章

  1. poj 3783 Balls 动态规划 100层楼投鸡蛋问题

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098409.html 题目链接:poj 3783 Balls 动态规划 100层楼投鸡蛋问题 ...

  2. POJ 3783 Balls --扔鸡蛋问题 经典DP

    题目链接 这个问题是谷歌面试题的加强版,面试题问的是100层楼2个鸡蛋最坏扔多少次:传送门. 下面我们来研究下这个题,B个鸡蛋M层楼扔多少次. 题意:给定B (B <= 50) 个一样的球,从 ...

  3. poj 3783

    Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1196   Accepted: 783 Description ...

  4. Labeling Balls POJ - 3687 优先队列 + 反向拓扑

    优先队列 + 反向拓扑 //#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include ...

  5. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  6. poj 3687 Labeling Balls - 贪心 - 拓扑排序

    Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N ...

  7. POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16100   Accepted: 4726 D ...

  8. POJ——T 3687 Labeling Balls

    http://poj.org/problem?id=3687 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14842   ...

  9. POJ 3687 Labeling Balls()

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...

随机推荐

  1. hiho1514 偶像的条件 lower_bound

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi的学校正面临着废校的大危机.面对学校的危机,小Hi同学们决定从ABC三个班中各挑出一名同学成为偶像. 成为偶像团体的 ...

  2. for循环使用append问题

    append添加到元素的时候,已存在的元素是移动而不是复制,使用了遍历,所以第一次是新增,后面都是移动前面新增的,最后当然就出现了只有最后一项有东西.解决办法:1.不要遍历,使用jQuery的类选择器 ...

  3. CF1117D Magic Gems

    CF1117D Magic Gems 考虑 \(dp\) , \(f[i]\) 表示用 \(i\) 个单位空间的方案数,答案即为 \(f[n]\). 对于一个位置,我们可以放 \(Magic\) 的, ...

  4. Navicat工具导出mySQL数据库某个视图结构的.sql脚本

    用Navicat工具怎么都导不出来mySQL数据库的某个视图.sql脚本,即使导出来也只是包含视图记录,不包含视图结构.经过一番研究,终于克服,操作如下: 1.在某个数据库中,新建备份,如下图 2.选 ...

  5. 「GXOI / GZOI2019」简要题解

    「GXOI / GZOI2019」简要题解 LOJ#3083. 「GXOI / GZOI2019」与或和 https://loj.ac/problem/3083 题意:求一个矩阵的所有子矩阵的与和 和 ...

  6. win32窗口样式GWL_EXSTYLE

    Private Const GWL_STYLE = (-16)             '窗口样式 '窗口风格Private Const WS_CAPTION = &HC00000       ...

  7. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

  8. lua中的数学库

    Lua5.1中数学库的所有函数如下表: 使用数学库,不需要require,直接math.function就可以math.pi 为圆周率常量 = 3.14159265358979323846 abs 取 ...

  9. 网站SEO知识

    http://seo.chinaz.com/ 这是综合查询的 site:www.hr246.com 可以查看百度收录的情况 参照贪玩游戏来搞 http://www.tanwan.com http:// ...

  10. Java API 操作Zookeeper

    一.依赖 <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookee ...