The Frog's Games

来源:互联网 发布:造纸术的影响 知乎 编辑:程序博客网 时间:2024/06/05 23:02

点击打开题目

给你一个传送门:点击打开链接

The Frog's Games

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 5707    Accepted Submission(s): 2748


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 2225 3 311 218
 

Sample Output
411
 

Source
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest



在 l / m 到 l 上二分求解,依次往最远处跳,用chck()函数判断。


代码如下:

[cpp] view plain copy
 print?
  1. #include <cstdio>  
  2. #include <vector>  
  3. #include <algorithm>  
  4. using namespace std;  
  5. int l,n,m;      //河流长度,石头数,跳跃次数  
  6. int pos[500000+11];     //石头的位置   
  7. bool check(int x)  
  8. {  
  9.     int s = 0;      //已跳过的石头数   
  10.     for (int i = 1 ; i <= m ; i++)  
  11.     {  
  12.         s = upper_bound (pos , pos + n, pos[s] + x) - pos - 1;  
  13.         if (s >= n - 1)          //pos[n-1]为岸   
  14.             return true;  
  15.     }  
  16.     return false;  
  17. }  
  18. int main()  
  19. {  
  20.     int left,right,mid;  
  21.     while (~scanf ("%d %d %d",&l,&n,&m))  
  22.     {  
  23.         for (int i = 1 ; i <= n ; i++)  
  24.             scanf ("%d",&pos[i]);  
  25.         pos[0] = 0;  
  26.         pos[n+1] = l;  
  27.         n += 2;     //把起点终点算上,一共n+2个数   
  28.         sort (pos,pos+n);  
  29.         left = l / m;  
  30.         right = l;  
  31.         while (right >= left)  
  32.         {  
  33.             mid = (left + right) >> 1;  
  34.             if (check(mid))  
  35.                 right = mid - 1;  
  36.             else  
  37.                 left = mid + 1;  
  38.         }  
  39.         printf ("%d\n",left);  
  40.     }  
  41.     return 0;  
  42. }  
原创粉丝点击