CodeForces - 551C(二分)

Description

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.

Examples

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.

呃,不得不说二分这个思想真的很强大,比赛的时候我是想了半天也没想出来是用二分写的,下来后看了几篇博客才看出来是怎么回事然后写个代码出了个bug,结果找了半天。这道题的思路是用二分法从后往前推,为什么呢,你想每个人时间是一样的,要么去搬箱子,要么走在路上,比如(4,5,6)这个序列表示相应位置的箱子数,如果第一个人把最后6个箱子搬完了,到第二个人的时候,第二个人直接到箱子为5这个位置和上个人相比少走了1步,而他少走的这1步就可以花在搬箱子上面,所以就从后往前遍历,最后出答案

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
const ll inf=1e18;
ll v[100005];
ll n,m,mid;
ll tp(ll mid)
{
    ll p,q;
    q=m;
    while(!v[q])
    {
        q--;//记录最后一个不为0箱子的位置为q
    }
    p=v[q];
    for(ll a=1;a<=n;a++)
    {
        ll k=mid; //每个人的时间都是一样的为二分取的那个值
        k-=q;  //总时间减去每个人走到最后那个箱子的时间就是就是花在搬箱子的时间
        if(k<=0)
        {
            return 0; //如果走到这个箱子的位置的时间不够的话那就更别说搬箱子了
        }
        while(k)
        {
            if(k>=p) //如果第a个人可以把这堆箱子搬走的话就直接搬了,然后剩余时间去搬前面箱子
            {
                k-=p;
                q--;
                while(!v[q])                
                    q--;  //每次换一下最后那个箱子的位置,比如 (6 7 8 )这个序列,如果前面一个人把8那个箱子搬完了,下一个人就不用再到8这个位置了,就直接到7这个位置,就少走了1步,用少走的那1秒用来搬箱子
                    if(q<1)
                    {
                    return 1;  //箱子全搬完了
                    }
                    p=v[q];
            }
            else  //如果不能全部搬走的话就尽量多搬
            {
                p-=k;
                break;
            }
        }
    }
    return 0;
}
int main()
{
    scanf("%lld%lld",&m,&n);
    for(ll a=1;a<=m;a++)
    {
        scanf("%lld",&v[a]);
    }
    ll r=0,l=inf;
    while(r<l)
    {
         mid=(r+l)/2;
        if(!tp(mid))
           r=mid+1;
        else
        {
            l=mid;
        }
    }
    printf("%lld\n",l); //此时输出r,l,mid都可以,它们相等
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44122831/article/details/89044247