为什么这道题mid要+1啊?
查看原帖
为什么这道题mid要+1啊?
859183
ReverSouth楼主2023/4/17 18:42

这是AC的:

while(l<r)
        {
            **LL mid=(l + r + 1) >> 1;**
            if(check(mid)<k)r=mid-1;
            else l=mid;
        }
        printf("%lld",l);

这是TLE的:

while(l<r)
        {
           	**LL mid=(l + r) >> 1;**
            if(check(mid)<k)r=mid-1;
            else l=mid;
        }
        printf("%lld",l);

check函数:


LL check(LL m)
{
    LL x=0,res=0;
    for(int i=1;i<=n;i++)
    {
        x=max(x+a[i],0ll);
        if(x>=m)
        {
            x=0;
            res++;
        }
    }
    return res;
}
2023/4/17 18:42
加载中...