Codeforces 460C prsent(二分答案)

时间:2021-11-01 21:03:19
//题意:给定N朵花的原先的高度,从左到右排列,
//最多浇水m天,每天只能浇一次,每次使得连续的w朵花的高度增长1,问最后最矮的花的高度最高是多少。
# include <stdio.h>
# include <algorithm>
# include <string.h>
using namespace std;
int main()
{
    __int64 n,m,w,l,r,i,m1,sum;
    __int64 a[200010],b[200010];
    while(~scanf("%I64d%I64d%I64d",&n,&m,&w))
    {
        for(i=0; i<n; i++)
            scanf("%I64d",&a[i]);
        l=0;
        r=2000000000;
        while(l<=r)
        {
            int mid=(l+r)/2;
            m1=0;
            sum=0;
            memset(b,0,sizeof(b));
            for(i=0; i<n&&m1<=m; i++)
            {
                sum=sum+b[i];
                if(sum+a[i]<mid)
                {
                    m1+=mid-(sum+a[i]);//到达mid所需的天数
                    b[i+w]-=mid-(sum+a[i]);//a[i+w]结束
                    sum=mid-a[i];
                }
            }
            if(m1<=m)
                l=mid+1;
            else
                r=mid-1;
        }
        printf("%I64d\n",l-1);
    }
    return 0;
}