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

题意: 青蛙举行了运动会,要求青蛙跳跃小河。河流宽度为 L (1<= L <= 1000000000). 河里会有 n 个石头沿着垂直于河岸的直线排成一排青蛙以跳到石头上,然后再次跳跃。青蛙最多能够跳 m 次;现在青蛙们想知道他们最少应该有多大的跳跃能力才能够到达河对岸?

思路:最多跳m次条件下,求最小的跳跃距离,那么这个距离应该满足至少能够跳过相邻的石头才可能到达对岸,二分答案。
如果跳不过去,肯定不行。如果当前的石头能够越过,但是跳不到下一个石头,跳的次数就+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 ){
			tot ++;
			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 ;
}

猜你喜欢

转载自blog.csdn.net/frankax/article/details/79926163