Balls
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 664 |
|
Accepted: 463 |
Description
The classic Glass Balls Brain-teaser is often posed as:
"Given-identical glass spheres, you would as-determine the lowest floor in a 100-story building from which they w Ill break when dropped. Assume the spheres is undamaged when dropped below this point. What's the strategy that would minimize the worst-case scenario for number of drops? "
Suppose that we had only one ball. We ' d have a to-drop from the 1 to sequence, requiring-drops in the worst case.
Now consider the case where we have both balls. Suppose we drop the first ball from floor N. If It breaks we ' re in the case where we had one ball remaining and we need to drop from floors 1 to n-1 in sequence, Yiel Ding n drops in the worst case (the first ball was dropped once, the second at most n-1 times). However, if it does not break when dropped from floor N, we had 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. The minimum number of drops, in the worst case, was the minimum over all n.
You'll write a program to determine the minimum number of drops required, in the worst case, given B balls and an M-stor Y building.
Input
The first line of input contains a single integer P, (1≤p≤1000), which are the number of data sets that follow. Each data set consists of a containing three (3) decimal integer values:the problem number, followed by a SPAC E, 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
Title Link: http://poj.org/problem?id=3783
The main topic: B Ball, M floor, there is a demarcation floor k, when the floor is greater than equal to K drop ball will be broken, ask in the worst case, throw at least a few times can determine this K
Title Analysis: Dp[i][j] indicates that the first layer of I have only J ball and in the worst case to determine the number of times K, then we can enumerate the middle K, assuming:
The K-layer falls broken then dp[i][j] = Dp[k-1][j-1] indicates that the K-level has been confirmed, there are k-1 layer, because a broken one, and left J-1
The k layer falls not broken then dp[i][j] = Dp[i-k][j] means that the layer K and below will not be broken, and the remaining i-k layer is not determined, because it is not broken, there is a J ball
Because we ask for the worst case of the minimum number of times, then dp[i][j] = Min (Dp[i][j],max (dp[k-1][j-1], dp[i-k][j]) + 1) plus 1 is because this throw ball is counted as one
According to the data 10^3 * 10^3 * 501 seconds is definitely T, the result 62ms on the water, offline incredibly also only used 157ms.
Offline:
#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 <=; j + +) dp[i][j] = INF; for (int i = 0; I <=; i++) dp[0][i] = 0; for (int i = 1, i <= 1001; i++) for (int j = 1; J <=; J + +) for (int k = 1; k <= i; k++) dp[i][j] = mi N (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]);} }
Online:
#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]);} }
POJ 3783 Balls (linear DP puzzle)