#include "ricehub.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Info{int l, r; ll cost;};
int besthub(int R, int L, int X[], long long B) {
const int n = R;
vector<ll> pre(X, X+n);
for (int i = 1; i < n; ++i) pre[i] += pre[i-1];
auto sum = [](const vector<ll>& ps, int l, int r)->long long{
if (l > r) return 0LL;
ll ans = ps[r];
if (l > 0) ans -= ps[l-1];
return ans;
};
auto get = [&sum, &n](int A[], int idx, int l, int r, const vector<ll>& ps)->Info{
int lb = lower_bound(A, A+n, l) - A;
int ub = upper_bound(A, A+n, r) - A - 1;
int le = 1+idx-lb;
int g = ub-idx;
return Info{lb, ub, 1LL*A[idx]*le-sum(ps, lb, idx) + sum(ps, 1+idx, ub)-1LL*g*A[idx]};
};
auto getleft = [&sum, &n](int p, int l, int r, const vector<ll>& ps)->ll{
const int len = 1+r-l;
return 1LL*p*len-sum(ps, l, r);
};
auto getright = [&sum, &n](int p, int l, int r, const vector<ll>& ps)->ll{
const int len = 1+r-l;
return sum(ps, l, r)-1LL*p*len;
};
int ans = 1;
for (int i = 0; i < n; ++i) {
Info best{-1,-1};
{
int low = 0, high = 1e9;
while (low <= high) {
int mid = low + ((high-low)/2);
Info x = get(X, i, X[i]-mid, X[i]+mid, pre);
if (x.cost <= B) {
low = 1+mid;
best = x;
} else {
high = mid-1;
}
}
}
assert(best.l != -1);
int len = 1+best.r-best.l, op1 = 0, op2 = 0;
ll rem = B - best.cost;
assert(rem >= 0);
{
int low = 0, high = best.l-1;
while (low <= high) {
int mid = (low+high) >> 1;
if (getleft(X[i], mid, best.l-1, pre) <= rem) {
high = mid-1;
op1 = best.l-mid;
} else {
low = 1+mid;
}
}
}
{
int low = best.r+1, high = n-1;
while (low <= high) {
int mid = (low+high) >> 1;
if (getright(X[i], best.r+1, mid, pre) <= rem) {
low = 1+mid;
op2 = mid-best.r;
} else {
high = mid-1;
}
}
}
ans = max(ans, len+max(op1, op2));
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 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 |
0 ms |
256 KB |
Output is correct |
11 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
288 KB |
Output is correct |
7 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
512 KB |
Output is correct |
2 |
Correct |
60 ms |
528 KB |
Output is correct |
3 |
Correct |
188 ms |
1536 KB |
Output is correct |
4 |
Correct |
234 ms |
1536 KB |
Output is correct |
5 |
Correct |
98 ms |
896 KB |
Output is correct |
6 |
Correct |
102 ms |
896 KB |
Output is correct |
7 |
Correct |
143 ms |
1536 KB |
Output is correct |
8 |
Correct |
200 ms |
1536 KB |
Output is correct |
9 |
Correct |
143 ms |
896 KB |
Output is correct |
10 |
Correct |
126 ms |
896 KB |
Output is correct |
11 |
Correct |
171 ms |
1536 KB |
Output is correct |
12 |
Correct |
249 ms |
1536 KB |
Output is correct |
13 |
Correct |
183 ms |
896 KB |
Output is correct |
14 |
Correct |
160 ms |
896 KB |
Output is correct |
15 |
Correct |
131 ms |
1152 KB |
Output is correct |
16 |
Correct |
175 ms |
1152 KB |
Output is correct |
17 |
Correct |
168 ms |
1408 KB |
Output is correct |
18 |
Correct |
215 ms |
1408 KB |
Output is correct |
19 |
Correct |
178 ms |
1400 KB |
Output is correct |
20 |
Correct |
227 ms |
1408 KB |
Output is correct |