题意:
有一栋nn层楼的建筑,一个鸡蛋在超过层楼的地方丢下去的时候会碎,现在有mm个鸡蛋,问你最坏情况下需要多少次才能知道的值。
思路:
设dp[x][tot]:dp[x][tot]:在xx层楼的时候还有个鸡蛋最少的实验次数,那么可以知道:
dp[x][tot]=min{ max(dp[i−1][tot−1]+1,dp[x−i][tot]+1) | 1⩽i⩽x−1 } dp[x][tot]=min{ max(dp[i−1][tot−1]+1,dp[x−i][tot]+1) | 1⩽i⩽x−1 }
dp[i−1][tot−1]dp[i−1][tot−1]代表在第ii层这个鸡蛋碎了,代表在第ii层这个鸡蛋没碎,取最大值是因为要取最坏情况下的值,这样直接考虑的话时间是,会超时,但是其实鸡蛋不需要很多就能测出位置了, 10001000层的时候只要1010个鸡蛋就够了,所以mm和取个最小值就行了
#include<bits/stdc++.h>
typedef long long ll;
const int maxn = 1e3 +10;
const int INF = 1e9 + 10;
using namespace std;
int n, m, T, kase = 1;
int dp[maxn][50];
///dp[x][tot] : 第x层还有tot个鸡蛋的实验最少次数
int dfs(int x, int tot) {
if(!x) return dp[x][tot] = 0;
if(!tot) return dp[x][tot] = INF; ///没有鸡蛋了
if(x == 1) return dp[x][tot] = 1; ///只有一层都还有鸡蛋
if(dp[x][tot] != -1) return dp[x][tot];
int &res = dp[x][tot];
res = dfs(x - 1, tot - 1) + 1; ///第x层丢下去碎了
for(int i = 1; i < x; i++) {
///第i层丢下去没碎
int si = dfs(x - i, tot) + 1;
///第i层丢下去碎了
int ei = dfs(i - 1, tot - 1) + 1;
res = min(res, max(si, ei)); ///最差情况
}
return res;
}
int main() {
memset(dp, -1, sizeof dp);
while(scanf("%d %d", &n, &m) && (n + m)) {
n = min(15, n);
int ans = dfs(m, n);
cout << ans << endl;
}
return 0;
}
本文探讨了一种经典的动态规划问题——鸡蛋掉落问题。通过定义状态dp[x][tot]表示在x层楼还有tot个鸡蛋时最少需要的实验次数,利用递归公式进行求解,并通过优化减少时间复杂度。
1921

被折叠的 条评论
为什么被折叠?



