GukiZ hates Boxes(二分)

GukiZ hates Boxes

这个题太难了,看别人的题解也不会,种草。

#include<bits/stdc++.h>
using namespace std;
long long a[100010];
int n,m,tot;
long long ans;
int cheak(long long x)
{
    long long s=0;
    int cnt=m;
    for(int i=1;i<=tot;i++)
    {
        s+=a[i];
        while(s+i>=x)//搬货时间加上到达时间大于X
        {
            s-=x-i;//对于已走的前i段一个人有x-i的时间搬货品,所以每增加一个人,搬货物的限制时间减少(x-i)
            cnt--;
            if(cnt<0) return 0;//时间不够
        }
    }
    if(cnt==0) return s<=0;//能考虑完所有人但是时间s<=0;即时间不够
    return 1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        long long s=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&a[i]);
            s+=a[i];
            if(a[i]) tot=i;
        }
        long long l=tot+1;//最少需要的时间
        long long r=tot+s;//最多需要的时间
        while(l<=r)
        {
            long long mid=(l+r)/2;
            if(cheak(mid))
            {
                ans=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        printf("%I64d\n",ans);
    }
}
发布了10 篇原创文章 · 获赞 1 · 访问量 161

猜你喜欢

转载自blog.csdn.net/weixin_45701902/article/details/105314582