codeforce 551C GukiZ hates Boxes(二分+贪心)

来源:互联网 发布:家庭教育网络培训平台 编辑:程序博客网 时间:2024/04/27 23:17
C. GukiZ hates Boxes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. If i ≠ n, move from pile i to pile i + 1;
  2. 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.

Examples
input
2 11 1
output
4
input
3 21 0 2
output
5
input
4 1003 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 (1second) 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.



题意:有n个空地(1~n),空地上有a[i]块石头,m个学生,每个学生在0这个位置,有两个操作均需要消耗1秒

操作1:将当前所在位置的石头删除

操作2:往右走一个位置,问删除所有空地上的石头需要花费的最少时间


二分枚举答案,贪心判断是否满足 即可。关键在于如何贪心,我们可以 从1向最后一个有石头的位置枚举(因为后面的都无关了,而且这样做方便后面的贪心),将中间遇到的石头都加起来,如果此时到当前位置所需花费时间+删除石头所需时间>=枚举值,这时候就需要一个学生,从0~枚举时间  对这段区间进行删除石头的操作,此时可以完美利用该学生,使其在枚举的时间内一刻不停地干活,剩下以此类推

#include<bits/stdc++.h>using namespace std;typedef long long ll;ll n,m,tot;ll a[100005];bool solve(ll limit){    ll cnt=m,sum=0,k=0;    for(int i=1;i<=tot;i++)    {        sum+=a[i];        while(sum+i>=limit)//如果>=枚举时间,就需要一个学生        {            sum-=limit-i;            cnt--;            //学生不够用了,枚举时间不满足            if(cnt<0)return false;        }    }    //学生数为0,且石头搬完返回真    if(cnt==0)return sum<=0;    //学生没用完,那就更应该返回真了    return true;}int main(){    ll sum=0,ans;    cin>>n>>m;    for(ll i=1; i<=n; i++)    {        cin>>a[i];        sum+=a[i];        if(a[i])tot=i;    }    ll l=tot,r=tot+sum,mid;    while(l<=r)    {        mid=(l+r)/2;        if(solve(mid))            {                ans=mid;                r=mid-1;            }        else            l=mid+1;    }    printf("%lld\n",ans);}



阅读全文
0 0
原创粉丝点击