#include "ricehub.h"
#include <algorithm>
#include <vector>
using namespace std;
vector < long long > all;
vector < long long > a,b;
int besthub(int R, int L, int X[], long long B)
{
long long now=0,i,j,ans=0,t=0,x,y=0;
for(i=0;i<R;i++)
{
a.push_back(X[i]-X[0]);
if(i) a[i]+=a[i-1];
}
for(i=0;i<R;i++)
{
b.push_back(X[R-1]-X[i]);
if(i) b[i]+=b[i-1];
}
ans=max(upper_bound(a.begin(),a.end(),B)-a.begin(),upper_bound(b.begin(),b.end(),B)-b.begin());
a.clear();
b.clear();
now=0;
for(i=0;i<R;i++)
{
if(now+(X[i]-X[0])>B)
{
t=i;
x=i;
break;
}
now+=X[i]-X[0];
}
//printf("0 %lld %lld %lld %lld\n",x,y,t,now);
y=0;
ans=max(ans,t);
for(i=1;i<R;i++)
{
now+=(X[i]-X[i-1])*(y+1);
now-=(X[i]-X[i-1])*(x-1);
y++;
x--;
//printf("aa %lld %lld %lld %lld\n",i,x,y,now);
if(x==0)
{
x++;
t++;
}
while(1)
{
//if(x<=1||y<=0) break;
//printf("%lld %lld\n",X[i+x]-X[i],X[i]-X[i-y]);
if(x!=1&&i-y-1>=0&&X[i+x-1]-X[i]>=X[i]-X[i-y-1])
{
now-=X[i+x-1]-X[i];
now+=X[i]-X[i-y-1];
x--;
y++;
}
else if(y!=0&&i+x<R&&X[i+x]-X[i]<=X[i]-X[i-y])
{
now+=X[i+x]-X[i];
now-=X[i]-X[i-y];
y--;
x++;
}
else break;
}
//printf("vv %lld %lld %lld %lld\n",i,x,y,now);
while(now>B)
{
if(y==0)
{
now-=X[i+x-1]-X[i];
x--;
t--;
}
else if(X[i+x-1]-X[i]<=X[i]-X[i-y])
{
now-=X[i]-X[i-y];
y--;
t--;
}
else
{
now-=X[i+x-1]-X[i];
x--;
t--;
}
else break;
}
while(now<=B)
{
if(i+x>=R&&i-y-1>=0)
{
if(now+X[i]-X[i-y-1]<=B)
{
y++;
t++;
now+=X[i]-X[i-y-1];
}
else break;
}
else if(i+x<R&&i-y-1<0)
{
if(now+X[i+x]-X[i]<=B)
{
x++;
t++;
now+=X[i+x]-X[i];
}
else break;
}
else if(i+x<R&&i-y-1>=0)
{
if(X[i+x]-X[i]<=X[i]-X[i-y-1])
{
if(now+X[i+x]-X[i]<=B)
{
x++;
t++;
now+=X[i+x]-X[i];
}
else break;
}
else
{
if(now+X[i]-X[i-y-1]<=B)
{
y++;
t++;
now+=X[i]-X[i-y-1];
}
else break;
}
}
else break;
}
//printf("%lld %lld %lld %lld\n",i,x,y,now);
if(now<=B) ans=max(ans,t);
}
return (int) ans;
}
Compilation message
ricehub.cpp: In function 'int besthub(int, int, int*, long long int)':
ricehub.cpp:93:13: error: 'else' without a previous 'if'
93 | else break;
| ^~~~
ricehub.cpp:9:23: warning: unused variable 'j' [-Wunused-variable]
9 | long long now=0,i,j,ans=0,t=0,x,y=0;
| ^