Skip to content

Commit a371eb7

Browse files
committed
atcoder/abc023D
1 parent 168ea18 commit a371eb7

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

atcoder/abc023/D/main.cpp

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
11
#include <bits/stdc++.h>
22

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+
317
using namespace std;
418
using ll = int64_t;
519
using ff = long double;
620

721
int N;
822
vector<ll> H, S;
923

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];
3129
}
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);
3341
}
3442

3543
int main() {

0 commit comments

Comments
 (0)