Topic Description: (directly extract the original online sentence)
One way to evaluate the hardness of an egg is to measure how high the egg will break down. Jiajia wants to evaluate the hardness of eggs Based on the floor height. If the egg falls down from the upper floor of I, it will break down, and the I-1 will not, then the hardness of the egg is I. There are eggs sold in the n-storey Lab Building, and an x RMB. Jia started to have no eggs, and he could only carry one egg with him. Without carrying the egg into the building, he needed Y yuan and Z yuan for carrying the egg. If there is another egg after the test, it can be sold at X/2 RMB (no need to enter the building to sell eggs ). Jia threw the eggs out and checked the eggs out of the building. If the egg is not broken after it is dropped, Jia will definitely pick up the egg and enter the building. If the egg is broken, Jia will not care about it. Jia wants to know the minimum cost of measuring the hardness of eggs in the worst case.
--------------------------------------------------
Question:
When analyzing the sample, we thought there were only three strategies: binary, top-down, bottom-up.
Later, it was no longer valid when it came. Because the above three policies only apply to extreme conditions. If xyz is close, the problem is not that simple.
After careful analysis, we can find that the problem can be divided and solved with a very beautiful dp.
Analysis can be found, from 1 .. n-1 and 2 .. the n-layer tests the hardness of eggs separately, and the worst result value should be the same (assuming both do not bring eggs into the building at first ). That is to say, the problem has nothing to do with the specific floor, but is only related to the total number of floors.
Therefore, when dp [I] is set to the total number of floors to be tested as I (in this case, the I layer is the highest level by default, and the eggs will be broken), no eggs are carried at the beginning, and when standing outside the building, minimum Cost of detecting the egg hardness in the worst case.
When we still have layer I to be tested, let's assume we dropped it from layer j (j <I). If the egg is broken, then the j-layer should be checked (the j-layer becomes the highest floor, and the egg will be broken by default, which complies with the definition of dp [I]), then it will be transferred from dp [I; if the eggs are not broken, the I-j layers are left to be checked (layer j is changed to layer 0th, which is not broken by default and still complies with the dp [I] definition ), however, because the eggs are not broken at this time, the eggs should be carried in, while the dp [I] definition is not to carry the eggs in, then
Dp [I-j]-(x + y) + z transfer, that is, the cost of bringing eggs into the building and Buying eggs, plus the cost of bringing eggs into the building.
That is, dp [I] = max (dp [j], dp [I-j]-(x + y) + z) (j <I ).
In addition, the boundary value. Dp [1] is obviously 0. When I-j is 1, that is, when n-1 layers of eggs are not broken, we obviously know that the hardness is n-1, and no need to detect it, the dead egg can be sold, and it becomes dp [I] = max (dp [j],-x/2) (j = i-1 ).
The key to this question is to understand that the result is irrelevant to the floor on which you are located, but is related to the number of floors to be tested. And clarify the boundary value.
--------------------------------------------------
Source code:
[Cpp]
# Include <stdio. h>
# Include <iostream>
# Include <string. h>
# Include <algorithm>
# Include <math. h>
# Include <string>
# Include <map>
Using namespace std;
# Define maxn1010
# Define INF 1000000000
# Define max (a, B) (a)> (B )? (A) (B ))
# Define min (a, B) (a)> (B )? (B) ())
Int dp [maxn];
Int x = 0, y = 0, z = 0;
Void solve (int n)
{
Int I = 0, j = 0;
Int ans = 0;
Dp [1] = 0;
For (I = 2; I <= n; I ++)
{
Ans = INF;
For (j = 1; j <I; j ++)
{
Int t1 = dp [j];
Int t2 = dp [I-j]-x-y + z;
If (I-j = 1)
T2 =-x/2;
Ans = min (ans, max (t1, t2 ));
}
Dp [I] = ans + x + y;
}
}
Int main ()
{
Int n = 0, k = 0, t = 0;
Scanf ("% d", & t );
For (k = 1; k <= t; k ++)
{
Scanf ("% d", & n, & x, & y, & z );
Solve (n );
Printf ("Case % d: % d \ n", k, dp [n]);
}
Return 0;
}