hdu 4044 The Frog's Games

The Frog's Games

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


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

The meaning of the question: The frog held a sports meeting and asked the frog to jump in the river. The width of the river is L (1<= L <= 1000000000). There will be n stones in the river lined up along a line perpendicular to the river bank , the frogs can jump on the stones and then jump again . Frogs can jump at most m times; now the frogs want to know what is the minimum jumping ability they should have to get to the other side of the river?

Idea: Under the condition of jumping at most m times, find the minimum jumping distance, then this distance should satisfy at least jumping over the adjacent stones before reaching the opposite bank. The answer is two points.
If you can't jump over it, definitely not. If the current stone can be crossed, but the next stone cannot be jumped, the number of jumps will be +1.

Code:
#include <bits/stdc++.h>
#define LL long long
#define INF 1e9
using namespace std;
LL L , n ,m ;
const int AX = 5e5+666;
LL a[AX];

int f( int x ){
	int tot = 0;
	LL pre = 0;
	int i ;
	for( i = 0 ; i <= n+1 ; i++ ){
		if( a[i] - pre > x ) break;
		if( a[i] - pre <= x && a[i+1] - pre > x ){
			to ++;
			pre = a[i] ;
		}
	}
	return ( (i == n + 1 && tot <= m ) ? 1 : 0 );
}
int main(){
	while( scanf("%lld%lld%lld",&L,&n,&m) != EOF ){
		LL sum = 0LL;
		for( int i = 0 ; i < n ; i++ ){
			scanf("%lld",&a[i]);
		}
		sort( a , a + n );
		a[n] = L;
		a[n+1] = INF;
		LL l = 0 , r = L;
		while( l <= r ){
			LL mid = ( l + r ) >> 1;
			if( f(mid) ){
				r = mid - 1;
			}else{
				l = mid + 1;
			}
		}
		printf("%lld\n",l);
	}
	return 0 ;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325730808&siteId=291194637