#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 5;
ll f[N];
bool check (ll x, ll b, int l, int r, vector<ll> &a) {
int j = upper_bound(a.begin(), a.end(), x) - a.begin();
j = min(j, r - 1);
ll sum = x * (j - l + 1) - (f[j] - f[l - 1]) + (f[r] - f[j]) - x * (r - j);
// if (x == 8 && l == 2 && r == 5) {
// cout << f[r]- f[j] << '\n';
// cout << j + 1 << ' ' << sum << '\n';
// cout << (sum <= b) << '\n';
// }
return (sum <= b);
}
bool ok (int k, ll b, int n, vector<ll> &a) {
for (int i = k; i <= n; i++) {
ll med = (f[i] - f[i - k]) / k;
if (check(med, b, i - k + 1, i, a)) {
// if (k == 4) {
// cout << i - k + 1 << ' ' << i << '\n';
// }
return true;
}
if (check(med + 1, b, i - k + 1, i, a)) {
// if (k == 4) {
// cout << i - k + 1 << ' ' << i << '\n';
// }
return true;
}
if (check(med - 1, b, i - k + 1, i, a)) {
// if (k == 4) {
// cout << i - k + 1 << ' ' << i << ' ' << med - 1 << '\n';
// }
return true;
}
}
return false;
}
int besthub (int r, int l, ll[] x, ll b) {
vector<ll> a;
for (int i = 0; i < r; i++) {
f[i + 1] = f[i] + x[i];
a.push_back(x[i]);
}
int ans = -1;
int lo = 0, hi = r;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (ok(mid, b, r, a)) {
ans = mid;
lo = mid + 1;
}
else {
hi = mid - 1;
}
}
return ans;
}
// signed main() {
// ios_base::sync_with_stdio(0);
// cin.tie(0);
//
// cout << besthub(5, 20, {1, 2, 10, 12, 14}, 6);
//
// return 0;
// }
Compilation message
ricehub.cpp:45:33: error: expected ',' or '...' before 'x'
45 | int besthub (int r, int l, ll[] x, ll b) {
| ^
ricehub.cpp: In function 'int besthub(int, int, ll*)':
ricehub.cpp:48:27: error: 'x' was not declared in this scope
48 | f[i + 1] = f[i] + x[i];
| ^
ricehub.cpp:55:21: error: 'b' was not declared in this scope
55 | if (ok(mid, b, r, a)) {
| ^