1. 程式人生 > >HDU 4004 The Frog's Games(二分+小思維+用到了lower_bound)

HDU 4004 The Frog's Games(二分+小思維+用到了lower_bound)

project 等於 mis acc put ever line content should

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

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
題意:蛤蟆直線過河,直線上有石頭可以踩,告訴你河的寬度還有石頭個數還有最多可以跳幾次,每種過河方案都有一個單次跳的最大距離,讓你求出所有方案裏面單次跳的距離最小的那個方案,並輸出這個距離 從單次跳的最大距離下手進行二分,而過河最少跳一次,所以單次跳的最大距離應該就是河的跨度
#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)