HDU 4004 The Frog's Games(二分+小思维+用到了lower_bound)
Posted caiyishuai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 4004 The Frog's Games(二分+小思维+用到了lower_bound)相关的知识,希望对你有一定的参考价值。
The Frog‘s Games
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 9678 Accepted Submission(s): 4428
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
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> using namespace std; int a[500005]; int main() { int l,n,m; while(~scanf("%d %d %d",&l,&n,&m)) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } a[++n]=l;//把河的长度也放进去 sort(a+1,a+1+n);//排序 int ri=l,mid; int le=l/m; if(le*m<l)//为了保证以le的长度跳m次(不管石头的情况下)一定能到对岸 le++; int mm=l; while(le<=ri) { mid=(le+ri)/2; int sum=0; for(int i=1;i<=m;i++)//以mid的距离模拟跳m次看看能不能到 { int y=sum+mid;//直接跳能到达的距离 int p=lower_bound(a+1,a+1+n,y)-a;//p是这次最远能跳的石头编号+1 if(a[p]!=y&&(p==1||a[p]==sum))//要是p是当前的石头,代表这次不能跳到任何的石头上,失败 break; if(a[p]!=y)//因为是lower_bound(大于等于),所以如果不是y处有石头的话,要减一 p--; sum=a[p]; } if(sum!=l) le=mid+1; else if(sum==l) { if(mm>mid)//挑出最小的 mm=mid; ri=mid-1; } } printf("%d ",mm); } return 0; }
以上是关于HDU 4004 The Frog's Games(二分+小思维+用到了lower_bound)的主要内容,如果未能解决你的问题,请参考以下文章
HDU 4004 The Frog's Games(二分+小思维+用到了lower_bound)