[Solution] egg problem of hardness

Title Description

  XX company recently held a strange game: King Competition egg hardness. Participants hens from around the world, the content of the game is to see who in the hardest eggs, more surprising is XX company does not use any sophisticated instruments to measure the hardness of the egg, they used one of the most old-fashioned way - eggs thrown from a height - to test the hardness of the eggs, if eggs of hens from a first layer a high-rise not fall smashes, but from a + fell and broke when one layer, then say that hens eggs hardness is a. Of course you can find a variety of reasons why this method is unscientific, such as eggs hardness at the same hens may be different, and so on, but this does not affect the company's XX Championship, because they are only to attract everyone's attention, a eggs fall from the tower to 100 layers of time, this scenario is still able to attract a lot of people gathered to watch, of course, XX company also never forget to hang a banner in the high-rise, the words "XX company" written on - this game is just an alternative XX advertising company only.

  A little hard thinking from one thing always found a mathematical problem, it is no exception. "If there are a lot of the same hardness eggs, then I can use half way measure with the least number of times the hardness of the egg," a small A to own this conclusion very satisfied, but soon trouble coming, "but, if I the egg is not enough of it, such as I have only one egg, then I have to start throwing layer by layer from the first floor level, in the worst case I want to throw 100 times. If there are two eggs, then from local 2-storey start throw ...... and so on, no, it seems should start from third place to throw fishes, ah, ah seems not necessarily how to do ...... 3 eggs, 4, 5, more it ...... ", as usual, fell into a small a deadlock thinking, he is not so much hard thinking, but rather that he is like asking for trouble.

  Well, since the trouble came, someone would have to solve the trouble of A small depend on you to solve :)

 

Input Format

  Input includes a plurality of sets of data, each data line, comprising two positive integers n and m (1≤n≤100,1≤m≤10), where n represents the height of the building, m represents the number of eggs you now have, the eggs same hardness (i.e., they fall from a high place to either the same are not crushed or smashed), and less than equal to n. You can assume that the hardness of the egg from a height of less than or equal x where x fall in any case will not be broken (not broke eggs can continue to use), but certainly long throw from places higher than x broken.

  Each set of input data, you can assume that the hardness of eggs between 0 to n, i.e. in layer n + 1 will be broken eggs thrown.

 

Output Format

For each set of input, output an integer representing the number of throwing eggs using the optimal strategy in the worst case required.

 

SAMPLE INPUT

100 1

100 2

 

 

Sample Output

100

14

 

prompt

  Optimal strategy refers to the number of eggs thrown in the worst case the required minimum policy.

  If only one egg, you can only throw the first layer from the beginning, in the worst case, the hardness of the eggs is 100, so it is necessary to throw 100 times. If other strategies, you may be unable to measure the hardness of the eggs (for example, you first place the second layer of the throw, the result is broken, then you can not determine the hardness is 0 or 1), that is, in the worst case you We need to throw unlimited, so the answer to the first set of data is 100.

 

answer

  Easy to think, we have to set $ $ $ i $ floors, the number $ j $ eggs of dp [i] [j].

  We can enumerate $ k $ express throw an egg at the first floor of $ k $, if broken, then left $ (j - 1) $ eggs at this time of $ dp [i] [j] = dp [k - 1] [j - 1] + 1 $; if not broken, at this time $ dp [i] [j] = dp [i - k] [j] + 1 $, which we then take a larger value, i.e., for the worst.

  At the same time, we enumerate $ k $, in fact, is to enumerate optimal strategy.

  We get an equation: $ dp [i] [j] = min (dp [i] [j], max (dp [k - 1] [j - 1], dp [i - k] [j]) + 1) $.

#include <iostream>
#include <cstring> 

#define MAX_N (100 + 5)
#define MAX_M (10 + 5)

using namespace std;

int n, m;
int dp[MAX_N][MAX_M];

int main()
{
    while(cin >> n >> m)
    {
        for(register int i = 1; i <= n; ++i)
        {
            for(register int j = 1; j <= m; ++j)
            {
                dp[i][j] = i;
            }
        }
        for(register int i = 1; i <= n; ++i)
        {
            for(register int j = 2; j <= m; ++j)
            {
                for(register int k = 1; k <= i; ++k)
                {
                    dp[i][j] = min(dp[i][j], max(dp[k - 1][j - 1], dp[i - k][j]) + 1);
                }
            }
        }
        cout << dp[n][m] << "\n";
    }
    return 0;
}
Reference program

 

Guess you like

Origin www.cnblogs.com/kcn999/p/10959671.html