This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ll long long int
#define pii pair <int, int>
#define pb push_back
#define ff first
#define ss second
const int N = 1e5+5;
const ll inf = 1e18;
int P[N];
ll pref[N];
int F(int A, int B, int x){
int l = A, r = B, pos = A;
while(l <= r){
int md = (l+r)>>1;
if (P[md] <= x){
pos = md;
l = md + 1;
}else r = md - 1;
}
return pos;
}
ll solve(int A, int B){
ll answer = inf;
int l = P[A], r = P[B];
while(l <= r){
int md = (l+r)>>1;
int pos = F(A, B, md);
ll x = pref[pos], y = pref[B]-pref[pos];
x -= (A ? pref[A-1] : 0);
x = md*1ll*(pos-A+1)-x;
y = y-(B-pos)*1ll*md;
answer = min(answer, x+y);
if (x > y) r = md - 1;
else l = md + 1;
}
return answer;
}
int besthub(int n, int L, int X[], ll B){
pref[0] = X[0];
for (int i = 0; i < n; i++){
P[i] = X[i];
if (i) pref[i] = pref[i-1]+X[i];
}
int answer = 0, pos = 0;
for (int i = 0; i < n; i++){
while(solve(pos, i) > B) pos++;
answer = max(answer, i-pos+1);
}
return answer;
}
// int main ()
// {
// ios::sync_with_stdio(false);
// cin.tie(0);
// int n, L;
// ll B;
// cin >> n >> L >> B;
// int X[n];
// for (int i = 0; i < n; i++) cin >> X[i];
// cout << besthub(n, L, X, B);
// }
# | 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... |