您的位置:首页 > 其它

Codeforces 551C GukiZ hates Boxes【二分+思维处理】好题!

2017-01-30 17:42 567 查看
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:

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.

题目大意:

我们有M个人,一开始所有人都站在第一个地方的左边,每个地方都有ai个物品需要拿走,每秒每个人可以进行两种操作:

①从i走到i+1。

②除掉一个物品。

问最少需要多少秒将所有物品都去掉。

思路:

1、考虑到时间的单调性:时间越长,完成任务的进度就越大。

那么我们可以考虑二分枚举时间,对于当前枚举出来的量进行测试,对于结果继续寻找区间二分。

2、那么对于当前二分出来的值mid如何判断能否完成任务呢?

对于二分出来的值mid,是一个时间量,那么对于每一个人来讲,每一个人都是有mid这么些秒可以进行操作的。

那么我们不如一个人一个人的派出去,对于每一个人,都尽力完成任务(遇到一个ai>0的位子,就要努力去除物品。)

一点一点处理,时间复杂度O(M);我们每一个派出去的人之前完成的部分我们就没有必要再一个一个格子去走了,维护一个pos,表示上一个人最后停在的位子即可。

最后O(n)枚举,看看是否每个格子的物品都去除干净了,对于结果继续寻找二分区间即可。

AC代码:

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
#define ll __int64ll n,m,tot;
ll a[100060];
ll b[100060];
int Slove(ll mid)
{
for(int i=0;i<n;i++)a[i]=b[i];
int pos=0;
for(int i=1;i<=m;i++)
{
ll tim=mid-(pos+1);
while(tim>0)
{
if(pos==n)return 1;
while(a[pos]==0&&pos<n&&tim>0)pos++,tim--;
if(tim>0)
{
if(a[pos]>=tim)
{
a[pos]-=tim;
tim=0;
}
else
{
tim-=a[pos];
a[pos]=0;
}
}
else break;
}
}
for(int i=0;i<n;i++)
{
if(a[i]>0)return 0;
}
return 1;
}
int main()
{
while(~scanf("%I64d%I64d",&n,&m))
{
tot=0;
for(int i=0;i<n;i++)scanf("%I64d",&b[i]);
ll ans=-1;
ll l=0;
ll r=10000000000000000;
while(r-l>=0)
{
ll mid=(l+r)/2;
if(Slove(mid)==1)
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
printf("%I64d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 551C