HDU 4004 the frog's games (basic algorithm-greedy, search-binary)

Source: Internet
Author: User

The frog's gamesproblem descriptionthe 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 into ss the river, at least what ability shoshould 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 shoshould have.
Sample Input
6 1 2225 3 311 218
 
Sample output
411
 
Sourcethe 36th ACM/ICPC Asia Regional Dalian site -- online contest
Recommendlcy

Question:

There are n stones in the middle of a river with a length of L. It tells you where the frog can jump at least m times in the middle?


Solution:

The second part + greedy question, because the more powerful the frog's jumping ability, the less the number of times required, so the frog's jumping ability is monotonous, so the second part enumeration of the frog's jumping ability, when judging whether this capability can jump over, this can be solved with greed. Each time you jump to the far stone as much as possible under this capability, you can finally see if M has jumped to the other side.


Solution code:

#include <iostream>#include <cstdio>#include <vector>#include <algorithm>using namespace std;int L,n,m;vector <int> v;void input(){    int x;    v.clear();    v.push_back(0);    for(int i=0;i<n;i++){        scanf("%d",&x);        v.push_back(x);    }    v.push_back(L);    sort(v.begin(),v.end());}bool canJump(int dis){    int s=0,l=0,pos=0,vsize=v.size();    for(int i=0;i<m;i++){        while( l<vsize && v[l]<=s+dis){            pos=v[l];            l++;        }        s=pos;    }    if(s>=L) return true;    else return false;}void solve(){    int l=0,r=L;    while(l<r){        int mid=(l+r)/2;        if(canJump(mid)) r=mid;        else l=mid+1;    }    printf("%d\n",r);}int main(){    while(scanf("%d%d%d",&L,&n,&m)!=EOF){        input();        solve();    }    return 0;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.