您的位置:首页 > 其它

HDU-4004 二分

2017-07-16 08:13 288 查看
这一个是青蛙跳的题,就是他每次跳的位置进行二分,首先求出一个最大的相距离,然后再对每一次跳的距离二分,题目的要求是找一个跳到对岸的需要跳的最远的距离,跳的次数小于等于给的次数。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <math.h>
#include <stack>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 500000 + 10;
//const int inf = 1000000000 + 10;
int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int a[maxn];
int max1;
int M,L,N;
int judge(int k)
{
if(max1 > k)return 0;
int x = 0;
int cur = 1;
for(int i = 1;i <= N;i++)
{
if(a[i] - a[x] <= k && a[i + 1] - a[x] > k)
{
x = i;
cur++;
}
}
return cur<=M;

}
int main()
{
while(cin>>L>>N>>M)
{
max1 = 0;
memset(a,0,sizeof(a));
for(int i = 1;i <= N;i++)
{

scanf("%d",&a[i]);
}
a[N+1] = L;
sort(a+1,a+1+N);
for(int i = 1;i <= N + 1;i++)
{
max1 = max(max1,a[i] - a[i - 1]);
}
int l = 0,r = L,mid;
while(l < r)
{
mid = (l + r) / 2;
if(judge(mid))
{
r = mid;
}
else
l = mid + 1;
}
cout<<r<<endl;

}
return 0;

}


The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river
is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they 

are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog's longest jump distance).

InputThe input contains several cases. The first line of each case contains three positive integer L, n, and m. 

Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.
OutputFor each case, output a integer standing for the frog's ability at least they should have.
Sample Input
6 1 2
2
25 3 3
11
2
18


Sample Output
4
11
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: