Balls(poj 3783)
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)的更多相关文章
- poj 3783 Balls 动态规划 100层楼投鸡蛋问题
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098409.html 题目链接:poj 3783 Balls 动态规划 100层楼投鸡蛋问题 ...
- POJ 3783 Balls --扔鸡蛋问题 经典DP
题目链接 这个问题是谷歌面试题的加强版,面试题问的是100层楼2个鸡蛋最坏扔多少次:传送门. 下面我们来研究下这个题,B个鸡蛋M层楼扔多少次. 题意:给定B (B <= 50) 个一样的球,从 ...
- poj 3783
Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1196 Accepted: 783 Description ...
- Labeling Balls POJ - 3687 优先队列 + 反向拓扑
优先队列 + 反向拓扑 //#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include ...
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- 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 ...
- POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16100 Accepted: 4726 D ...
- POJ——T 3687 Labeling Balls
http://poj.org/problem?id=3687 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14842 ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
随机推荐
- LOJ2362. 「NOIP2016」蚯蚓【单调队列】
LINK 思路 良心来说这题还挺思维的 我没看题解也不知道要这样维护 把每次斩断的点分别放进两个队列里面 因为要维护增长,所以可以让新进队的节点来一个负增长? 是不是就好了? 然后很容易发现因为在原始 ...
- POJ2185 Milking Grid 【lcm】【KMP】
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- BZOJ3786 星系探索 【Splay维护dfs序】*
BZOJ3786 星系探索 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均 ...
- BZOJ5297 CQOI2018 社交网络 【矩阵树定理Matrix-Tree】
BZOJ5297 CQOI2018 社交网络 Description 当今社会,在社交网络上看朋友的消息已经成为许多人生活的一部分.通常,一个用户在社交网络上发布一条消息(例如微博.状态.Tweet等 ...
- MySQL之——查询重复记录、删除重复记录方法大全
查找所有重复标题的记录: SELECT * FROM t_info a WHERE ((SELECT COUNT(*) FROM t_info WHERE Title = a.Title) > ...
- Communications link failure,The last packet successfully received from the server was *** millisecon
使用Connector/J连接MySQL数据库,程序运行较长时间后就会报以下错误: Communications link failure,The last packet successfully r ...
- Kanboard 看板工具配置使用
备注: 类似的开源工具有wekan 界面还有功能和Trello 类似.比较方便 1. 安装(基于docker+ docker-compose) a. 安装docker && ...
- 模态对话框中的window.close关闭时会打开新页面
在模态对话框的页面的<head></head>加上<base target="_self"> 就不会打开新页面了.
- Python 函数 -getattr()
getattr(object, name[, default]) getatt() 函数用于返回一个对象属性值.object 对象.name 字符串,对象属性.object 默认返回值,如果不提供该参 ...
- ShowDialog窗体的return问题
最近的一个项目里,打开新窗口用到了ShowDialog()这种方式,发现在新窗口做保存操作的时候,保存按钮事件下的程序执行完(无论有没有return)都会关闭子窗口. 网上查了一下,发现大家说的方法在 ...