#include "ricehub.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
int besthub(int R, int L, int X[], i64 B) {
vector<i64> pre(R), suf(R);
pre[0] = suf.back() = 0;
for (int i = 1; i < R; i++) {
pre[i] = pre[i - 1] + (X[i] - X[i - 1]);
}
for (int i = R - 2; ~i; i--) {
suf[i] = suf[i + 1] + (X[i + 1] - X[i]);
}
vector<i64> dp_pre(R), dp_suf(R);
dp_pre[0] = dp_suf.back() = 0;
for (int i = 1; i < R; i++) {
dp_pre[i] = dp_pre[i - 1] + 1LL * i * (X[i] - X[i - 1]);
}
for (int i = R - 2; ~i; i--) {
const int T = R - i - 1;
dp_suf[i] = dp_suf[i + 1] + 1LL * T * (X[i + 1] - X[i]);
}
auto range_sum = [&](const vector<i64> &dp, const vector<i64> &sum, int lo, int hi, bool is_right) -> long long {
if (is_right) {
i64 a = dp[lo] - dp[hi];
i64 b = 1LL * (R - hi - 1) * (sum[lo] - sum[hi]);
return a - b;
}
i64 a = dp[hi] - dp[lo];
i64 b = 1LL * lo * (sum[hi] - sum[lo]);
return a - b;
};
auto ok = [&](int how_many) {
for (int i = 0; i <= R - how_many; i++) {
const int lo = i;
const int hi = i + how_many - 1;
const int mid = lo + (hi - lo) / 2;
i64 dist = 0;
// Right side
dist += range_sum(dp_suf, suf, mid, hi, true);
// Left side
dist += range_sum(dp_pre, pre, lo, mid, false);
if (dist <= B) return true;
}
return false;
};
int lo = 0, hi = R + 7;
while(lo < hi) {
const int mid = lo + (hi - lo) / 2;
if (!ok(mid)) hi = mid;
else lo = mid + 1;
}
return hi - 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1152 KB |
Output is correct |
2 |
Correct |
4 ms |
1152 KB |
Output is correct |
3 |
Correct |
29 ms |
4864 KB |
Output is correct |
4 |
Correct |
20 ms |
4864 KB |
Output is correct |
5 |
Correct |
11 ms |
2432 KB |
Output is correct |
6 |
Correct |
9 ms |
2432 KB |
Output is correct |
7 |
Correct |
16 ms |
4608 KB |
Output is correct |
8 |
Correct |
16 ms |
4608 KB |
Output is correct |
9 |
Correct |
9 ms |
2432 KB |
Output is correct |
10 |
Correct |
9 ms |
2432 KB |
Output is correct |
11 |
Correct |
21 ms |
4864 KB |
Output is correct |
12 |
Correct |
21 ms |
4864 KB |
Output is correct |
13 |
Correct |
11 ms |
2432 KB |
Output is correct |
14 |
Correct |
10 ms |
2432 KB |
Output is correct |
15 |
Correct |
15 ms |
3712 KB |
Output is correct |
16 |
Correct |
15 ms |
3712 KB |
Output is correct |
17 |
Correct |
18 ms |
4352 KB |
Output is correct |
18 |
Correct |
22 ms |
4352 KB |
Output is correct |
19 |
Correct |
20 ms |
4728 KB |
Output is correct |
20 |
Correct |
19 ms |
4736 KB |
Output is correct |