标签:input his pac art queue tac sid printf iostream
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 9678 Accepted Submission(s): 4428
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\n",mm); } return 0; }
HDU 4004 The Frog's Games(二分+小思维+用到了lower_bound)
标签:input his pac art queue tac sid printf iostream
原文地址:https://www.cnblogs.com/caiyishuai/p/9542460.html