# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
339830 | Sho10 | Rice Hub (IOI11_ricehub) | C++14 | 0 ms | 0 KiB |
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> //Andrei Alexandru a.k.a Sho10
#include "ricehub.h"
#define ll long long
#define double long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define all(a) (a).begin(), (a).end()
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 1000000007
#define PI 3.14159265359
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005ll
#define CODE_START ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,pref[200005],suf[200005],a[200005];
ll calc1(ll i,ll j){
return pref[j]-pref[i]-i*(a[j]-a[i]);
}
ll calc2(ll i,ll j){
return suf[i]-suf[j]-(n-j-1)*(a[j]-a[i]);
}
int32_t besthumb(int32_t R,int32_t L,int32_t X[],ll B){
n=R;
for(ll i=0;i<n;i++)
{
a[i]=X[i];
if(i>0){
pref[i]=pref[i-1]+i*(a[i]-a[i-1]);
}
}
for(ll i=n-1;i>=0;i--)
{
if(i<n-1){
suf[i]=suf[i+1]+(n-i-1)*(a[i+1]-a[i]);
}
}
ll ans=1;
for(ll i=0;i<n;i++){
ll l=0,r=n+1,mid;
while(l<r){
ll mid=(l+r)/2,x=0,y=0;
if(mid%2==1){
x=mid/2;
y=mid/2;
}else {
x=mid/2;
y=mid/2-1;
}
if(x>i||y>(n-i-1)){
r=mid;
continue;
}
ll sum=calc1(i-x,i)+calc2(i,i+y);
if(sum>B){
r=mid;
}else {
l=mid+1;
}
mid=(l+r)/2;
}
ll x=0,y=0;
if(mid%2==1){
x=mid/2;
y=mid/2;
}else {
x=mid/2;
y=mid/2-1;
}
ll sum=calc1(max(0ll,i-x),i)+calc2(i,i+y);
if(sum>B||x>i||y>(n-i-1)){
mid--;
}
ans=max(ans,mid);
}
return ans;
}
/*
int32_t main(){
CODE_START;
*/