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 "ricehub.h"
#include<bits/stdc++.h>
#define ll long long
using namespace std;
vector<ll> pref, arr;
ll getSum(int a, int b)
{
return pref[b] - pref[a - 1];
}
int besthub(int R, int L, int X[], long long B)
{
pref.resize(R + 1, 0);
arr.resize(R + 1);
for(int i=1;i<=R;i++)
pref[i] += pref[i - 1] + X[i - 1], arr[i] = X[i - 1];
int ans = -1;
// for(int i=1;i<=R;i++)
// {
// for(int j=i;j<=R;j++)
// {
// int mid = (i + j) / 2;
// ll cur = (arr[mid] * (mid - i) - getSum(i, mid - 1)) + (getSum(mid + 1, j) - arr[mid] * (j - mid));
// cout << cur << '\n';
// if(cur <= B)
// ans = max(j - i + 1, ans);
// }
// }
int i = 0, j = 0;
while(R - j > 0)
{
int mid = (i + j) / 2;
ll cur = (arr[mid] * (mid - i) - getSum(i, mid - 1)) + (getSum(mid + 1, j) - arr[mid] * (j - mid));
if(cur <= B)
ans = max(j - i + 1, ans), j++;
else
i++;
}
return ans;
}
# | 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... |