您的位置:首页 > 其它

CodeForces - 551C (二分查找答案+贪心)

2017-12-03 17:27 302 查看
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 th
aa5d
e boxes from the last pile is finished.

题意:一个老师在去学校的路上有很多箱子在路上挡路,他的学生们去把箱子搬走,学生们走一步需要一秒,搬一个箱子需要一秒。现在这些学生需要你给出最短的时间来搬完这些箱子。

这道题的思路是这样的,枚举时间,用这个时间来看箱子是否搬完,枚举出最小的哪一个,由于数据很大,我们用二分来实现。然后是看怎么来贪心了,题目看了就是贪心,但是怎么贪就很烦。我们可以这么想:

这一群学生可以单独行动,但是他们都要尽力完成自己的任务,我们要从后往前搬,因为如果从前往后搬,假如你搬完了a[i]堆时省了一秒,那么你这一秒只能前往下一堆箱子,下个人过来时也同样浪费一秒在了跑路上而不是搬箱子。如果我们从后往前搬,那么后边来的人就可以多把时间放在搬箱子上。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m;
int a[100005];
int x;
int aa(long long mid)
{
int i=1,z=x,k=a[x];
long long t;
while(i<=m)
{
t=mid;
if(t<=z) return 0;
t-=z;
while(t>=k)
{
t-=k;
z--;
while(z>=1&&!a[z]) z--;
if(z==0) return 1;
k=a[z];
}
k-=t;
i++;
}
return 0;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
if(a[i]) x=i;
}
long long l=0,r=10000000000000000,mid;
while(l<r)
{
mid=(l+r)/2;
if(aa(mid)) r=mid;
else l=mid+1;
}
printf("%I64d\n",r);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: