# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
339479 | bigDuck | 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 "ricehub.h"
#include<bits/stdc++.h>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll
int d1[100010], d2[100010];
int x[100010], n;
int sum1(int i, int j){
return d1[j]-d1[i]-(i)*(x[j]-x[i]);
}
int sum2(int i, int j){
return d2[i]-d2[j]-(n-j-1)*(x[j]-x[i]);
}
int besthub(int R, int L, int X[], long long B)
{
n=R;
for(int i=0; i<R; i++){
x[i]=X[i];
if(i>0){
d1[i]=d1[i-1]+(i)*(x[i]-x[i-1]);
}
// cout<<d1[i]<<" ";
}
//cout<<"\n";
for(int i=R-1; i>=0; i--){
if(i<(R-1)){
d2[i]=d2[i+1]+(n-(i+1))*(x[i+1]-x[i]);
}
//cout<<d2[i]<<" ";
}
//cout<<"\n";
int res=0;
for(int i=0; i<R; i++){
int l=0, r=R, m=(l+r)>>1ll;
while(l<r){
m=(l+r)>>1ll;
int lt=0, rt=0;
if( (m%2)==1){
lt=m>>1ll; rt=m>>1ll;
}
else{
rt=m>>1ll; lt=(m>>1ll)-1;
}
if( (lt>(i)) || (rt>(R-(i+1))) ){
r=m; continue;
}
//cout<<"c :"<<m<<" |";
//cout<<"s "<<sum1(i-lt, i)<<" "<<sum2(i, i+rt)<<"\n";
int sum=sum1(i-lt, i)+sum2(i, i+rt);
if(sum>=B){
r=m;
}
else{
l=(m+1);
}
m=(l+r)>>1ll;
}
int lt=0, rt=0;
if( (m%2)==1){
lt=m>>1ll; rt=m>>1ll;
}
else{
rt=m>>1ll; lt=(m>>1ll)-1;
}
int sum=sum1(max(i-lt, 0ll), i)+sum2(i, i+rt);
if( (sum>B) || ( (lt>(i)) || (rt>(R-(i+1))) ) ){
m--;
}
res=max(res, m);
//cout<<m<<"\n";
}
return res;
}