File tree Expand file tree Collapse file tree 1 file changed +30
-22
lines changed Expand file tree Collapse file tree 1 file changed +30
-22
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
18
using ll = int64_t ;
5
19
using ff = long double ;
6
20
7
21
int N;
8
22
vector<ll> H, S;
9
23
10
- ll solve () {
11
- const ll inf = numeric_limits<ll>::max ();
12
- ll ans = 0 ;
13
- // O(N**2)
14
- vector<bool > done (N, false );
15
- for (int t = N - 1 ; t >= 0 ; --t) {
16
- int min_index;
17
- ll min_height = inf;
18
- for (int i = 0 ; i < N; ++i) {
19
- if (done[i]) continue ;
20
- ll height = H[i] + t * S[i];
21
- bool should_update =
22
- height < min_height ||
23
- (height == min_height && S[min_index] > S[i]);
24
- if (should_update) {
25
- min_height = height;
26
- min_index = i;
27
- }
28
- }
29
- ans = max (ans, min_height);
30
- done[min_index] = true ;
24
+ vector<ll> ts;
25
+ bool pass (ll const X) {
26
+ for (int i = 0 ; i < N; ++i) {
27
+ if (X - H[i] < 0 ) return false ;
28
+ ts[i] = (X - H[i]) / S[i];
31
29
}
32
- return ans;
30
+ sort (ts.begin (), ts.end ());
31
+ for (int i = 0 ; i < N; ++i) {
32
+ if (i > ts[i]) return false ;
33
+ }
34
+ return true ;
35
+ }
36
+
37
+ ll solve () {
38
+ ts.assign (N, 0 );
39
+ ll ng = 0 , ok = numeric_limits<ll>::max () / 10 ;
40
+ return binary_search (pass, ng, ok);
33
41
}
34
42
35
43
int main () {
You can’t perform that action at this time.
0 commit comments