GukiZ hates Boxes 二分

来源:互联网 发布:按键精灵源码库 编辑:程序博客网 时间:2024/05/16 18:02

Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way.

In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≤ i ≤ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are:

If i ≠ n, move from pile i to pile i + 1;
If pile located at the position of student is not empty, remove one box from it.
GukiZ’s students aren’t smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn’t want to wait). They ask you to calculate minumum time t in seconds for which they can remove all the boxes from GukiZ’s way. Note that students can be positioned in any manner after t seconds, but all the boxes must be removed.

Input
The first line contains two integers n and m (1 ≤ n, m ≤ 105), the number of piles of boxes and the number of GukiZ’s students.

The second line contains n integers a1, a2, … an (0 ≤ ai ≤ 109) where ai represents the number of boxes on i-th pile. It’s guaranteed that at least one pile of is non-empty.

Output
In a single line, print one number, minimum time needed to remove all the boxes in seconds.

Example
Input
2 1
1 1
Output
4
Input
3 2
1 0 2
Output
5
Input
4 100
3 4 5 4
Output
5
Note
First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second).

Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds.

Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished.

第一个学生第一秒向→走,第二秒删a[1]的一块石头
第二个学生一直走到头,删掉a[3] ,所以第二个学生花费是 1+1+1+2 = 5
两个学生可以同时运动。

思路:
二分答案,设答案为x,即需要x秒来搬完石头
先拿一个学生来
总要有一个学生来搬最远的那堆石头的
先让这个学生走到尽头
这个学生拥有的时间就是x
那么走到尽头后,还剩下的时间用来搬最后一堆石头
如果这堆石头搬不完,那么这个学生就利用完了,换一个学生
如果搬的完 那么这个学生还剩下的时间可以用来搬前一堆石头 一直把这个学生利用完。

如果所有学生都利用完了石头还是搬不完,那么x秒肯定是不够的
否则x秒就是够的

#include <bits/stdc++.h>using namespace std;int a[101010];int b[101001];typedef long long ll;int n,m;bool ok(ll x){    for(int i=1;i<=n;i++) b[i]=a[i];        int top=n,tmp=m;    while(tmp-->0&&top) //大循环是每一个人同时堆数不等于0    {        ll lef = x-top; //把步骤简单化        while(lef&&top)//这个是总判断条件也是下面子部分的判定条件        {            if(b[top]==0) {top--;continue;} //和总的判定条件相同            if(b[top]<=lef)            {                lef-=b[top--];            }            else { b[top]-=(int)lef;lef=0;}        }    }    while(top&&b[top]==0) top--; //找到最后一个不是0的点    return top==0;//最后判定条件是top==0}int main(){    scanf("%d%d",&n,&m);    for(int i=1;i<=n;i++)        scanf("%d",&a[i]);    while(a[n]==0) n--;    long long ans=0;    long long l=1;long long r=1e15;    while(l<=r)    {        long long mid=(l+r)>>1;        if(ok(mid))        {            ans=mid;            r=mid-1;        }        else l=mid+1;    }    printf("%lld\n",ans );}
0 0
原创粉丝点击