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 层楼需要的最少判断次数其实是一样的)。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stack>
  4. #include<queue>
  5. #include<iostream>
  6. #include<algorithm>
  7. #include<map>
  8. #include<vector>
  9. #define PI acos(-1.0)
  10. using namespace std;
  11. typedef long long ll;
  12. const int Inf=0x3f3f3f3f;
  13. int m,n,k,p;
  14. int str[];
  15. int ans[];
  16. int dp[][];////dp[i][j]:表示在 i 层楼 还有 j 个鸡蛋的最小判断次数
  17. int di[][]={{-,},{,},{,-},{,}};
  18. map<ll,ll>::iterator it;
  19. void solve(int p,int k)
  20. {
  21. memset(dp,,sizeof(dp));
  22. for(int i=;i<=k;i++)
  23. {
  24. dp[i][]=i;//只有一个鸡蛋的情况
  25. }
  26. for(int i=;i<=p;i++)
  27. {
  28. dp[][i]=;//只有一层楼的情况
  29. }
  30. for(int i=;i<=k;i++)
  31. for(int j=;j<=p;j++)
  32. {
  33. dp[i][j]=Inf;
  34. for(int t=;t<=i;t++)
  35. {
  36. dp[i][j]=min(dp[i][j],max(dp[t-][j-],dp[i-t][j])+);
  37. }
  38. }
  39. cout<<n<<" "<<dp[k][p]<<endl;
  40. }
  41. int main()
  42. {
  43. cin>>m;
  44. while(m--)
  45. {
  46. cin>>n>>p>>k;
  47. solve(p,k);
  48. }
  49. }

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. LOJ2321. 「清华集训 2017」无限之环【费用流】

    LINK 很好的一道网络里题 首先想插头DP的还是出门左转10分代码吧 然后考虑怎么网络流 首先要保证没有漏水 也就是说每个接口一定要有对应的接口 那么发现每个点只有可能和上下左右四个点产生联通关系 ...

  2. Codeforces 1030D 【构造】

    LINK 题目大意:给你n,m,k,让你在一个n*m的点阵里构造出一个面积为\(\frac{n*m}{k}\)的三角形 思路 首先要有一个结论是整点三角形的面积分母最多为2,然后就可以判断不存在的情况 ...

  3. 【模板】NTT

    NTT模板 #include<bits/stdc++.h> using namespace std; #define LL long long const int MAXL=22; con ...

  4. webdriver常用函数总结

    #1 创建浏览器对象 driver = webdriver.Chrome() #2 设置隐式等待10秒 driver.implicitly_wait(10) #3 最大化浏览器窗口 driver.ma ...

  5. C#/.NET 中推荐的 Dispose 模式的实现

    如果你觉得你的类需要实现 IDisposable 接口,还是需要注意一些坑的.不过前人准备了 Dispose 模式 供我们参考,最大程度避免这样的坑. C#程序中的 Dispose 方法,一旦被调用了 ...

  6. 接口测试基础——第7篇 Python中_、__、__func__之间的区别

    今天的东西很少,主要是给自己做个笔记,顺便帮大家普及一下Python中的边角知识: 1.if __name__ == "__main__"是什么意思 答:一个.py文件,如果是自身 ...

  7. 开始创建一个 Vue 项目

    开始创建一个 Vue 项目 安装 nodejs 略 安装 npm 默认安装时自带了 npm 安装 cnpm 为了更快的下载组件,使用cnpm,cnpm 是淘宝前端的镜像. 使用 npm 安装 cnpm ...

  8. js前台调用lodop打印

    lodop简单介绍 lodop的打印功能已经非常强大,也在带web端的图形界面,可以供用户使用.使用js在前台调用lodop打印,一般分为两种方法: 1:特殊的指令打印,这种打印方式,是采用的与js无 ...

  9. C/C++中一些不太注意到的小知识点--[锦集]

    C/C++中一些不太注意到的小知识点--[锦集] C/C++小知识点--[锦集] "="和"<=" 的优先级 1.( (file_got_len = re ...

  10. python 有关引用的一些问题

    python 有关引用的一些问题 print id.__doc__ ​ id(object) -> integer Return the identity of an object. This ...