一叶落寞,万物失色。
The classic Two Glass Balls brain-teaser is often posed as:
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.
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 题意:给定B (B <= 50) 个一样的球,从 M (M <= 1000) 层楼上一个一个往下扔,存在某个楼层K,使得低于它的楼层往下扔球,球不会碎,在第K层扔下去会碎。求最坏情况下,需要扔几次才能确定这个K。 思路: p[i][j] 表示有i层楼, 剩余j个球时, 最坏情况要确定K 所需的次数那么在这些楼层里
我们可以选择在k层(1<= k <= i)扔
有两种情况,破跟不破
(1)不破, 则排除掉了k层,剩余i-k层 则转化为 dp[i - k][j]
(2)破了 则剩余i - 1层, 球剩k - 1个 转化为 dp[i - 1][ k - 1]
然后由于是最坏情况,所以对于特定的 k
dp[i][j] = min(dp[i][j], max(dp[i - k][j], dp[i - 1][ k - 1]) + 1 )
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <cmath> #include <algorithm> #include <map> #include <ctime> #define MAXN 111111 #define MAXM 1122222 #define INF 1000000001 #define eps 1e-8 using namespace std; int dp[1111][55]; int main() { int T, cas, n, k; scanf("%d", &T); while(T--) { scanf("%d%d%d", &cas, &k, &n); memset(dp, 0x3f, sizeof(dp)); for(int i = 0; i <= k; i++) dp[0][i] = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= k; j++) { for(int p = 1; p <= i; p++) { int z = max(dp[i - p][j], dp[p - 1][j - 1]) + 1; dp[i][j] = min(dp[i][j], z); } } } printf("%d %d\n", cas, dp[n][k]); } return 0; }