【bzoj2600】 [Ioi2011]ricehub
如果发现尾指针到头指针这段稻田的中位数上建一个粮仓时距离之和超过了B
就调整尾指针对距离维护一个前缀和
每次取中位数之后可以O(1)计算距离和
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> using namespace std; typedef long long LL; #define N 100010 int n,q; int x[N]; LL sum[N],ans,b; int work(int l,int r) { int mid=l+((r-l+1)>>1); LL sum1=(LL)(mid-l)*x[mid]-(sum[mid]-sum[l]); LL sum2=(sum[r]-sum[mid])-(LL)(r-mid)*x[mid]; return sum1+sum2>b; } int main() { scanf("%d%d%lld",&n,&q,&b); for (int i=1;i<=n;i++) scanf("%d",&x[i]),sum[i]=sum[i-1]+x[i]; for (int i=1,j=0;i<=n;i++) { while (work(j,i)) j++; ans=max(ans,(LL)i-j); } printf("%lld\n",ans); return 0; }