链接:https://www.nowcoder.com/acm/contest/106/K
来源:牛客网
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 di 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 (di+di+1+...+dj-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.
Then the next line N-1 positive integer followed, indicating the distance between two adjacent stone.
输出描述:
An integer, the minimum distance of the largest step.
示例1
输入
6 3
1 3 2 2 5
输出
来源:牛客网
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 di 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 (di+di+1+...+dj-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.
Then the next line N-1 positive integer followed, indicating the distance between two adjacent stone.
输出描述:
An integer, the minimum distance of the largest step.
示例1
输入
6 3
1 3 2 2 5
输出
5
这道题的坑点在于要用到long'long
代码:
#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[100010];
bool check(long long x)
{
int num=1;
long long sum=0;
for(int i=1;i<=n;i++)
{
if(sum+a[i]<=x)
sum+=a[i];
else
{
num++;
sum=a[i];
}
}
return num<=k;//返回1:说明x取大了或者取得正好
}
int main()
{
scanf("%d%d",&n,&k);
long long left=0;
long long right=0;
long long mid;
for(int i=1;i<=n-1;i++)
{
scanf("%d",&a[i]);
left=max(left,(long long)a[i]);
right+=a[i];
}
while(left<right)
{
mid=(left+right)/2;
if(check(mid)==1)
right=mid;
else
left=mid+1;
}
cout<<left<<endl;
return 0;
}
/*
6 3
1 3 2 2 5
*/