# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1128370 | lucaskojima | Rice Hub (IOI11_ricehub) | C++17 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#include "ricehub.h"
#define int long long
using namespace std;
int besthub(int n, int x, int vv[], int c) {
vector<int> v(n + 1);
for (int i = 1; i <= n; i++)
v[i] = vv[i - 1];
vector<int> 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;
int sumL = pref[m - 1] - pref[l - 1];
int sumR = pref[r] - pref[m];
int 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;
}