File tree Expand file tree Collapse file tree 1 file changed +18
-13
lines changed Expand file tree Collapse file tree 1 file changed +18
-13
lines changed Original file line number Diff line number Diff line change 1
1
#include < bits/stdc++.h>
2
2
3
+ inline int64_t binary_search (
4
+ std::function<bool (int64_t )> const & predicate,
5
+ int64_t ng,
6
+ int64_t ok
7
+ ) {
8
+ assert (!predicate (ng) && predicate (ok));
9
+ while (abs (ok - ng) > 1 ) {
10
+ auto m = (ok + ng) / 2 ;
11
+ if (predicate (m)) ok = m;
12
+ else ng = m;
13
+ }
14
+ return ok;
15
+ }
16
+
3
17
using namespace std ;
4
- using ll = long long ;
18
+ using ll = int64_t ;
5
19
6
20
int N, A, B;
7
21
vector<int > H;
8
22
9
- bool is_ok (const ll M) {
23
+ bool satisfy (const ll M) {
10
24
int mxh = *max_element (H.begin (), H.end ());
11
25
if (mxh / B < M) return true ; // prevent an overflow
12
26
assert (B * M <= mxh);
@@ -25,17 +39,8 @@ bool is_ok(const ll M) {
25
39
}
26
40
27
41
ll solve () {
28
- ll low = 0 , high = 1e15 ;
29
- assert (!is_ok (low) && is_ok (high));
30
- while (high - low > 1 ) {
31
- auto m = low + (high - low) / 2 ;
32
- if (is_ok (m)) {
33
- high = m;
34
- } else {
35
- low = m;
36
- }
37
- }
38
- return high;
42
+ ll ng = 0 , ok = 1e15 ;
43
+ return binary_search (satisfy, ng, ok);
39
44
}
40
45
41
46
int main () {
You can’t perform that action at this time.
0 commit comments