Files
python/TangDou/CSP-J/J1_2022/CSP_J22.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

43 lines
1.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int dp[N][N];
//在n层楼,剩余m个鸡蛋,返回最少的尝试次数
int f(int n, int m) {
if (m == 1) return n; //如果只有一个鸡蛋,那么需要枚举n层楼
if (n == 0) return 0; //如果没有楼,就不用再试了
if (dp[n][m]) return dp[n][m]; //计算过就返回
int ret = INT_MAX;
for (int i = 1; i <= n; i++) //在当前场景下,尝试每一层楼进行扔鸡蛋,把所有可能计算出来,挑一个尝试次数最小的
// f(n - i, m):鸡蛋没碎,楼层太矮,继续在i+1~n 这n-i层楼进行尝试,还有m个鸡蛋
// f(i - 1, m - 1):鸡蛋碎了,楼层太高,继续在1~i-1层楼进行尝试
// +1:代价是使用了一个次数
ret = min(ret, max(f(n - i, m), f(i - 1, m - 1)) + 1); //
return dp[n][m] = ret;
}
int g(int n, int m) {
for (int i = 1; i <= n; i++) dp[i][1] = i;
for (int j = 1; j <= m; j++) dp[0][j] = 0;
for (int i = 1; i <= n; i++)
for (int j = 2; j <= m; j++) {
dp[i][j] = INT_MAX;
for (int k = 1; k <= i; k++)
dp[i][j] = min(dp[i][j], max(dp[i - k][j], dp[k - 1][j - 1]) + 1);
}
return dp[n][m];
}
int main() {
int n, m;
// cin >> n >> m;
// cout << f(n, m) << endl
// << g(n, m) << endl;
cout << f(15, 3) << endl;
return 0;
}