It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
Now you're going to walk through a large forest. There is a path consisting of N stones winding its way to the other side of the forest. Between every two stones there is a distance. Let d
i indicates the distance between the stone i and i+1.Initially you stand at the first stone, and your target is the N-th stone. You must stand in a stone all the time, and you can stride over arbitrary number of stones in one step. If you stepped from the stone i to the stone j, you stride a span of (d
i+d
i+1+...+d
j-1). But there is a limitation. You're so tired that you want to walk through the forest in no more than K steps. And to walk more comfortably, you have to minimize the distance of largest step.
输入描述:
The first line contains two integer N and K
as described above.
![](https://i-blog.csdnimg.cn/blog_migrate/e3dc4a83487225eb8e0e78e28bf297df.png)
Then the next line N-1 positive integer followed, indicating the distance between two adjacent stone
.
![](https://i-blog.csdnimg.cn/blog_migrate/9bc6d35b1c69b1f99608a142b9440e3d.png)
输出描述:
An integer, the minimum distance of the largest step.
示例1
输入
6 3 1 3 2 2 5
输出
5
题意: 输入 n,k 下面为 n-1个数,为n个石头相邻的距离, 在不超过 他可以跳任意距离,求 在跳的不超k下的,所跳的最大距离的最小值;
比赛时提交错了,比赛后 把错误的代码又提交了一次 过了,无语。。。
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 100010
#define ll long long
ll a[Max];
ll n,m;
int main()
{
ll i,j;
while(~scanf("%lld%lld",&n,&m))
{
ll Ma = -1;
ll sum = 0;
for(i = 0;i<n-1;i++)
{
scanf("%lld",&a[i]);
Ma = max(Ma,a[i]);
sum += a[i];
}
ll flag = 1;
ll l = Ma,r = sum;
while(l<=r)
{
ll mid = (l+r)/2;
sum = 0;
ll num = 0;
for(i = 0;i<n;i++)
{
if(sum+a[i]>mid)
{
num ++;
sum = 0;
sum +=a[i];
}
else sum += a[i];
}
num++;
if(num<=m)
{
flag = mid;
r = mid - 1;
}
else
{
l = mid + 1;
}
}
printf("%lld\n",flag);
}
return 0;
}