您的位置:首页 > 其它

hrbust 1584 青蛙过河(二分)

2017-05-03 12:29 218 查看
青蛙过河
Time Limit: 1000 MSMemory Limit: 65536 K
Total Submit: 68(38 users)Total Accepted: 36(32 users)Rating: 







Special Judge: No
Description
青蛙王国一年一度的游戏又开始了,这个游戏要求青蛙必须跳过河。河的宽度是 L 。河里有n块石头,这n块石头从河的一边笔直的连到另一边。青蛙只能踩着石头过河,如果它们掉到水里,将被淘汰出局。游戏规定青蛙最多跳m次。现在青蛙想要知道如果在这m步内跳到岸的那边,它一步最长需要跳多长。
Input
输入包括多组测试结果。
第一行输入三个数字L(1<= L <= 1000 000 000),n(0<= n <= 500000),m(1<=
m <= n+1)。
接下来一行有n个用空格隔开的整数,表示每块石头到跳跃起点的距离,两块石头不可能同时出现在一个地方。
Output
对于每次测试,输出一个整数表示青蛙至少应该有的最大的能力,即为一步最多能跳多长,每步实际跳的长度一定小于等于这个最小的最大能力。
Sample Input
6 1 2
2
25 3 3
11 2 18
Sample Output
4

11

Author
彭文文@Amber
题解 :
二分答案就好了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[500003];
int main()
{
int L,n,k;
while(~scanf("%d%d%d",&L,&n,&k))
{
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
}
int r=1000000000;
int l=0;
a[0]=0;
a[n+1]=L;
sort(a,a+n+2);
/** for(int i=0;i<n+2;i++)
{
printf("%d ",a[i]);
}**/
//printf("\n");
//continue;
for(int i=1; i<=n+1; i++)
{
l=max(l,a[i]-a[i-1]);
}
int ans=r;
while(l<=r)
{
int m=(l+r)&g
4000
t;>1;
int t=1;
int dis=m;
for(int i=1; i<=n+1; i++)
{
if(a[i]-a[i-1]<=dis)
{
dis-=(a[i]-a[i-1]);
}
else
{
dis=m;
t++;
dis-=(a[i]-a[i-1]);
}
}
if(t<=k)
{
// printf("%d %d \n",t,m);
ans=min(m,ans);
r=m-1;

}
else
{
l=m+1;
}
}
printf("%d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: