POJ 3783 Balls (線性dp 智力題),poj3783
Balls
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 664 |
|
Accepted: 463 |
Description
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 42 143 244 10
Source
Greater New York Regional 2009
題目連結:http://poj.org/problem?id=3783
題目大意:b個球,m層樓,有一個分界樓層k,當樓層大於等於k時扔下球球會碎,問在最壞的情況下,最少扔幾次能確定這個k
題目分析:dp[i][j]表示第i層還剩j個球且在最壞的情況下確定k需要的次數,則我們可以枚舉中間的k,假設:
第k層落下碎了則dp[i][j] = dp[k - 1][j - 1]表示第k層確認過了,還有k-1層,因為碎了一個,還剩j-1個
第k層落下沒碎則dp[i][j] = dp[i - k][j]表示第k層及以下的層扔下去都不會碎,還剩i-k層未確定,因為沒碎,還有j個球
因為我們要求最壞的情況下的最小次數,則dp[i][j] = min(dp[i][j],max(dp[k - 1][j - 1], dp[i - k][j]) + 1) 加1是因為本次扔球也算作一次
按這題的資料10^3 * 10^3 * 50一秒肯定T,結果62ms就水過了,離線居然也只用了157ms。
離線:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int const INF = 0xffffff;int dp[1005][55];void cal(){ for(int i = 0; i <= 1001; i++) for(int j = 0; j <= 51; j++) dp[i][j] = INF; for(int i = 0; i <= 51; i++) dp[0][i] = 0; for(int i = 1; i <= 1001; i++) for(int j = 1; j <= 51; j++) 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);}int main(){ int T, ca, b, m; cal(); scanf("%d", &T); while(T--) { scanf("%d %d %d", &ca, &b, &m); printf("%d %d\n", ca, dp[m][b]); }}
線上:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int const INF = 0xffffff;int dp[1005][55];int main(){ int T, ca, b, m; scanf("%d", &T); while(T--) { scanf("%d %d %d", &ca, &b, &m); for(int i = 0; i <= m; i++) for(int j = 0; j <= b; j++) dp[i][j] = INF; for(int i = 0; i <= b; i++) dp[0][i] = 0; for(int i = 1; i <= m; i++) for(int j = 1; j <= b; j++) 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); printf("%d %d\n", ca, dp[m][b]); }}