#include <bits/stdc++.h>
#include "ricehub.h"
using namespace std;
using ll = long long;
int besthub(int n, int x, int vv[], ll c) {
vector<ll> v(n + 1);
for (int i = 1; i <= n; i++)
v[i] = vv[i - 1];
vector<ll> pref(n + 1);
for (int i = 1; i <= n; i++)
pref[i] = pref[i - 1] + v[i];
auto ok = [&](int k) -> bool {
for (int l = 1, r = k; r <= n; l++, r++) {
int m = (l + r) / 2;
ll sumL = pref[m - 1] - pref[l - 1];
ll sumR = pref[r] - pref[m];
ll cost = abs(v[m] * (m - l) - sumL) + abs(sumR - v[m] * (r - m));
if (cost <= c)
return true;
}
return false;
};
int l = 1; // l is good
int r = n + 1; // r is bad
while (r > l + 1) {
int m = (l + r) / 2;
ok(m) ? l = m : r = m;
}
return l;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |