2020NYIST个人积分赛第八场-F

题面

原题链接:HDU 3281

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 个鸡蛋。那么在最坏情况下我们最少需要做多少次实验呢?

思路

这是一个比较经典的 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 <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
int dp[2020][2020];//表示在第i层楼,还有j个鸡蛋的最小判断次数
int main()
{
	int op, m, n, t;
	cin >> t;
	while(t--)
	{
		cin >> op >> m >> n;//m个鸡蛋 n层楼
		memset(dp, 0, sizeof(dp));//初始化
		for (int i = 1; i <= n;i++)
			dp[i][1] = i;//1个鸡蛋时,从一楼往上,
		for (int i = 1; i <= m;i++)
			dp[1][m] = 1;//只有1个鸡蛋,不管是几楼都是1
		for (int i = 1; i <= n;i++)//楼层数
		{
			for (int j = 2; j <= m;j++)//鸡蛋数
			{
				dp[i][j] = INF;//初始化一个比较大的值
				for (int k = 1; k <= i;k++)//小于等于当前楼层数
					dp[i][j] = min(dp[i][j], max(dp[k - 1][j - 1], dp[i - k][j]) + 1);
			//对小于i层的每一层都遍历一下,找到扔鸡蛋的最小楼层。例如现在在第i层,有j个鸡蛋,状态表示为dp[i][j];
			//如果鸡蛋碎了,就从第一层遍历到i-1层,找到次数最小的一层。比如扔到k层时鸡蛋碎了,那么子问题就转化为
			//你有j-1个鸡蛋,和一个k-1层高的楼;如果鸡蛋没碎,子问题就转化为现在你有j个鸡蛋,和i-k层楼。
			}
		}
			cout << op << " " << dp[n][m] << endl;
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值