hdu 4004(The Frog's Games) 二分查找

Problem Description
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 2
2
25 3 3
11
2
18
 

 

Sample Output
4
11
 

 

Source
 
 
【二分查找】一种将庞大数据折半查找的思想,效率较高。
算法思想和我们玩的一种猜数游戏类似,比如我们猜一个100以内的数,(当我们猜数比被猜数大时或小时,会有大小提示),所以第一次我们一定会猜50,如果我们要猜的数是24,那么当提示比被猜数大时,我们一定就会猜25这样,进行一次查找并根据提示便能筛选出一半的数据,效率很高。
 
关于本题,由于数据给出的L较大(1000000000)所以如果逐个枚举青蛙的跳跃距离是会超时的。就要用到二分的思想。
题目大意:青蛙要度过一条河,河中横着放着n块石头,青蛙可以最多跳m次(落在石头上),问青蛙要度过河需要具备的最短的跳跃能力。
可以分析出青蛙跳跃的范围:   max(len[i]-len[i-1])<jump<L    max(len[i]-len[i-1])为相邻两块石头之间的距离(包括河的岸)
PS:有第二组数据可以看出需要对石块的距离进行排序。
对满足题目的条件的判定也很重要。
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 #define N 500005
 7 int len[N];
 8 int l,n,m;
 9 bool handle(int x)
10 {
11     int cur=0;
12     int step=0;
13     int i;
14     for(i=1;i<=n+1;i++)
15     {
16         if(cur+x<len[i])
17         {
18             cur=len[i-1];
19             step++;
20         }
21     }
22     if(step+1>m||cur+x<l)
23         return false;
24     else
25         return true;
26 }
27 
28 
29 
30 int main()
31 {
32     while(scanf("%d%d%d",&l,&n,&m)!=EOF)
33     {
34         int i;
35         memset(len,0,sizeof(len));
36         len[0]=0;
37         for(i=1;i<=n;i++)
38         scanf("%d",&len[i]);
39         len[n+1]=l;
40         sort(len+1,len+n+1);
41         int jump;
42         jump=0;
43         for(i=1;i<=n+1;i++)
44         {
45             if(len[i]-len[i-1]>jump)
46                 jump=len[i]-len[i-1];
47         }
48         int low,hight,mid;
49         low=jump;
50         hight=l;
51         int ans;
52         while(low<=hight)
53         {
54             mid=(low+hight)/2;
55             
56             if(handle(mid)==true)
57             {   
58                 ans=mid;
59                 hight=mid-1;
60             }
61             else
62                 low=mid+1;
63         }
64         printf("%d\n",ans);
65     }
66     return 0;
67 }

 

 

你可能感兴趣的:(games)