ural 1223. Chernobyl’ Eagle on a Roof
1223. Chernobyl’ Eagle on a Roof
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Once upon a time an Eagle made a nest on the roof of a very large building. Time went by and some eggs appeared in the nest. There was a sunny day, and Niels Bohr was walking on the roof. He suddenly said: “Oops! All eggs surely have the same solidity, thus there is such non-negative number E that if one drops an egg from the floor number E, it will not be broken (and so for all the floors below the E-th), but if one drops it from the floor number E+1, the egg will be broken (and the same for every floor higher, than the E-th).” Now Professor Bohr is going to organize a series of experiments (i.e. drops). The goal of the experiments is to determine the constant E. It is evident that number E may be found by dropping eggs sequentially floor by floor from the lowest one. But there are other strategies to find E for sure with much less amount of experiments. You are to find the least number of eggs droppings, which is sufficient to find number E for sure, even in the worst case. Note that dropped eggs that are not broken can be used again in following experiments.
The floors are numbered with positive integers starting from 1. If an egg has been broken being dropped from the first floor, you should consider that E is equal to zero. If an egg hasn’t been broken even being dropped from the highest floor, consider that E is also determined and equal to the total number of floors.
Input
Input contains multiple (up to 1000) test cases. Each line is a test case. Each test case consists of two numbers separated with a space: the number of eggs, and the number of floors. Both numbers are positive and do not exceed 1000. Tests will end with the line containing two zeroes.
Output
For each test case output in a separate line the minimal number of experiments, which Niels Bohr will have to make even in the worst case.
Sample
input | output |
---|---|
1 10 2 5 0 0 |
10 3 |
Problem Author: folklore, proposed by Alexander Klepinin, text by Nikita Shamgunov
Problem Source: The Seventh Ural State University collegiate programming contest
Problem Source: The Seventh Ural State University collegiate programming contest
Tags: dynamic programming
Difficulty: 346
题意:给n个鹰蛋,m层的楼,如果鹰蛋在k层碎了,k-1层不碎,那么鹰蛋的硬度为k
如果第1层碎了,硬度为零;如果第m层都不碎,那么硬度为m
问最坏情况下,用最优策略,最多能够测出多少的硬度
分析:这题可以用dp解决。
dp[i][j]表示用i个鹰蛋,j层楼最多能测出多少硬度
那么因为要将其化成子问题
对于当前的这个鹰蛋
枚举它在第k层的情况
如果它碎了,那么就用剩下i-1个鹰蛋去测k-1的楼层
若果不碎,相当于用i个鹰蛋去测j-k的楼层(即上面的楼层)(因为问的是最多,所以一定鹰蛋,对于一定楼层的答案是一定的,不用在意这是第几层)
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <iostream> 9 #include <algorithm> 10 #include <map> 11 #include <set> 12 #include <ctime> 13 using namespace std; 14 typedef long long LL; 15 typedef double DB; 16 #define For(i, s, t) for(int i = (s); i <= (t); i++) 17 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 18 #define Rep(i, t) for(int i = (0); i < (t); i++) 19 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 20 #define rep(i, x, t) for(int i = (x); i < (t); i++) 21 #define MIT (2147483647) 22 #define INF (1000000001) 23 #define MLL (1000000000000000001LL) 24 #define sz(x) ((int) (x).size()) 25 #define clr(x, y) memset(x, y, sizeof(x)) 26 #define puf push_front 27 #define pub push_back 28 #define pof pop_front 29 #define pob pop_back 30 #define ft first 31 #define sd second 32 #define mk make_pair 33 inline void SetIO(string Name) { 34 string Input = Name+".in", 35 Output = Name+".out"; 36 freopen(Input.c_str(), "r", stdin), 37 freopen(Output.c_str(), "w", stdout); 38 } 39 40 inline int Getint() { 41 int Ret = 0; 42 char Ch = ' '; 43 while(!(Ch >= '0' && Ch <= '9')) Ch = getchar(); 44 while(Ch >= '0' && Ch <= '9') { 45 Ret = Ret*10+Ch-'0'; 46 Ch = getchar(); 47 } 48 return Ret; 49 } 50 51 const int N = 3010, M = 12; 52 int n, m; 53 int Dp[M][N]; 54 bool Visit[M][N]; 55 56 inline void Solve(); 57 58 inline void Input() { 59 while(~scanf("%d%d", &n, &m) && n && m) { 60 Solve(); 61 } 62 } 63 64 inline int Search(int Egg, int Floor) { 65 Egg = min(Egg, 10); 66 if(!Egg || !Floor) return 0; 67 if(Egg == 1) return Floor; 68 if(Floor == 1) return 1; 69 if(Visit[Egg][Floor]) return Dp[Egg][Floor]; 70 Visit[Egg][Floor] = 1; 71 int Cnt = INF; 72 Dp[Egg][Floor] = 1; 73 Ford(i, Floor, 1) 74 Cnt = min(Cnt, max(Search(Egg-1, i-1), Search(Egg, Floor-i))); 75 Dp[Egg][Floor] += Cnt; 76 return Dp[Egg][Floor]; 77 } 78 79 inline void Solve() { 80 int Ans = Search(n, m); 81 printf("%d\n", Ans); 82 } 83 84 int main() { 85 #ifndef ONLINE_JUDGE 86 SetIO("G"); 87 #endif 88 Input(); 89 //Solve(); 90 return 0; 91 }