# [LeeCode 887. 鸡蛋掉落] DP+二分

## 1. 题目链接

[LeeCode 887. 鸡蛋掉落]

## 3. 解题思路

$$dp[i][j]=\mathrm{min}\{\mathrm{max}(dp[i-1][j-k],dp[i][k])+1\}$$

## 4. 参考代码

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const long double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const ll infl = 0x3f3f3f3f3f3f3f3f;

int dp[105][10005];

class Solution {
public:
int superEggDrop(int K, int N) {
memset(dp, 0x3f, sizeof(dp));
for(int i = 0; i <= K; ++i) dp[i][1] = 0;
for(int j = 1; j <= N + 1; ++j) dp[1][j] = j - 1;
for(int i = 2; i <= K; ++i) {
for(int j = 2; j <= N + 1; ++j) {
//for(int m = j/2; m <= j; ++m)
//dp[i][j] = min(dp[i][j], max(dp[i - 1][j - m], dp[i][m]) + 1);
/*
int lb = 0, ub = j, lmd, rmd;
while(lb <= ub) {
lmd = (lb + ub) >> 1, rmd = (lmd + ub) >> 1;
int lval = max(dp[i - 1][j - lmd], dp[i][lmd]);
int rval = max(dp[i - 1][j - rmd], dp[i][rmd]);
dp[i][j] = min(lval, rval) + 1;
if(lval == rval) lb = lmd + 1, ub = rmd - 1;
else if(lval < rval) ub = rmd;
else lb = lmd;
}
if(0 <= lb && lb <= j) dp[i][j] = min(dp[i][j], max(dp[i - 1][j - lb], dp[i][lb]) + 1);
if(0 <= ub && ub <= j) dp[i][j] = min(dp[i][j], max(dp[i - 1][j - ub], dp[i][ub]) + 1);
*/
int lb = 0, ub = j;
while(lb <= ub) {
int md = (lb + ub) >> 1;
dp[i][j] = min(dp[i][j], max(dp[i - 1][j - md], dp[i][md]) + 1);
if(dp[i - 1][j - md] == dp[i][md]) lb = md + 1, ub = md - 1;
else if(dp[i - 1][j - md] > dp[i][md]) lb = md + 1;
else ub = md - 1;
}
}
}
return dp[K][N + 1];
}
};

#ifdef __LOCAL_WONZY__
int main() {
freopen("input.txt", "r", stdin);
Solution solu;
cout << solu.superEggDrop(1, 2) << endl;
cout << solu.superEggDrop(2, 6) << endl;
cout << solu.superEggDrop(3, 14) << endl;
cout << solu.superEggDrop(3, 200) << endl;
return 0;
}
#endif