-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.h
76 lines (66 loc) · 2.05 KB
/
solution.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
template <class T>
auto vect(const T& v, int n) { return vector<T>(n, v); }
template <class T, class... D>
auto vect(const T& v, int n, D... m) {
return vector<decltype(vect(v, m...))>(n, vect(v, m...));
}
template <typename T>
static constexpr T inf = numeric_limits<T>::max() / 2;
mt19937_64 mrand(random_device{}());
long long rnd(long long x) { return mrand() % x; }
int lg2(long long x) { return sizeof(long long) * 8 - 1 - __builtin_clzll(x); }
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef pair<int, int> pii;
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
class Solution {
public:
int superEggDrop(int k, int n) {
auto dp = vect(inf<int>, k + 1, n + 1);
for (int i = 1; i <= n; i++) {
dp[1][i] = i;
}
for (int i = 1; i <= k; i++) {
dp[i][0] = 0;
}
auto work = [&](int x, int y) {
int l = 1, r = y;
int ans = l;
while (l <= r) {
int mid = l + r >> 1;
if (dp[x - 1][mid - 1] <= dp[x][y - mid]) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
debug(x, y, ans);
int tmp1 = 1 + dp[x][y - ans];
if (ans + 1 <= y) {
tmp1 = min(tmp1, 1 + dp[x - 1][ans]);
}
return tmp1;
};
for (int i = 2; i <= k; i++) {
dp[i][1] = 1;
for (int j = 2; j <= n; j++) {
int val = work(i, j);
dp[i][j] = val;
debug(i, j, dp[i][j]);
}
}
return dp[k][n];
}
};