HDU-4004 二分

来源:互联网 发布:怎样用dos运行java文件 编辑:程序博客网 时间:2024/05/21 09:49

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

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <math.h>#include <stack>#define LL long longusing 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).
Input
The 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.
Output
For each case, output a integer standing for the frog's ability at least they should have.
Sample Input
6 1 2225 3 311 218
Sample Output
411


原创粉丝点击