D - The Frog's Games (二分)
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).
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
题意:第一次提交的时候因为理解错题意WA,
一只青蛙要过一条河,给出了河的宽度,石头距离岸边的位置,和规定的调的步数
求青蛙最远要调的距离的最小值
AC代码:
1 #include<stdio.h> 2 #include<algorithm> 3 #include<string.h> 4 using namespace std; 5 6 int l, n, m, a[500005]; 7 int left, mid, right; 8 9 bool solve(int x) 10 { 11 int last = 0; 12 int num = 0; 13 for(int i = 0; i <= n;) 14 { 15 if(a[i] <= last+mid) 16 i++; 17 else 18 { 19 if(last == a[i-1]) 20 return false; 21 last = a[i-1]; 22 num++; 23 } 24 } 25 num++; //跳到最后一块石头上后还要再跳一下才能到达岸上; 26 return num <= m; 27 } 28 29 int main() 30 { 31 int ans; 32 while(~scanf("%d%d%d", &l, &n, &m)) 33 { 34 memset(a, 0, sizeof(a)); 35 for(int i = 0; i < n; i++) 36 scanf("%d", &a[i]); 37 a[n] = l; 38 sort(a, a+n+1); 39 40 left = 1; 41 right = l; 42 43 while(left <= right) 44 { 45 mid = (left + right) / 2; 46 if(solve(mid)) 47 { 48 ans = mid; 49 right = mid - 1; 50 } 51 else 52 left = mid + 1; 53 } 54 printf("%d\n", ans); 55 } 56 57 return 0; 58 }
永远渴望,大智若愚(stay hungry, stay foolish)