POJ-3258 River Hopscotch (二分)

Description

Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).

To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).

FJ wants to know exactly how much he can increase the shortest distance before he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.

Input

Line 1: Three space-separated integers: L, N, and M
Lines 2…N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output

Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks

Sample Input

25 5 2
2
14
11
21
17

Sample Output

4

Hint

Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).

翻译

起点是一块石头,终点是另一块石头,终点离起点有L个单位(1≤ L≤ 1,000,000,000). 沿着河流的起点和终点岩石之间有N(0≤ N≤ 50000)个岩石出现,每一个岩石与起点的积分距离为Di(0<Di<L)。
每头母牛依次从起跑的岩石开始,并试图在终点岩石到达终点,只从一块岩石跳到另一块岩石。当然,不那么敏捷的奶牛永远不会到达最后一块岩石,而是在河里。
农夫约翰为他的奶牛感到自豪,每年都会观看这一活动。但随着时间的推移,他厌倦了看着其他农民胆小的母牛蹒跚地穿过距离太近的岩石。他计划移走几块石头,以增加母牛必须跳到终点的最短距离。他知道自己无法移除起始和结束岩石,移除M个石头。
FJ想知道在开始移除岩石之前,他能在最短距离内增加多少。帮助农夫约翰确定在移除最佳M块岩石后奶牛必须跳跃的最大可能最短距离。
输入
第1行:三个空格分隔的整数:L、N和M
第2…N+1行:每行包含一个整数,表示某个岩石距离起始岩石的距离。没有两块石头的位置相同。
输出
第1行:一个整数,是奶牛在移除M块岩石后必须跳跃的最短距离的最大值

代码

#include "stdio.h"
#include "algorithm"
#include "string.h"
using namespace std;
int a[50099];
int n,m;
int ppp(int t)
{
    
    
	int i=0,s=0;
	for(int l=1;l<=n;l++)
	{
    
    
		if(a[l]-a[i]>=t) i=l;//下面 解释
		else s++;
	}
	if(s<=m) return 1;
	else return 0;
}
int main()
{
    
    
	int i;
	scanf("%d%d%d",&i,&n,&m);
	a[0]=0;
	a[++n]=i;
	for(i=1;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n+1);
	int k=1,j=a[n]-a[0],s;
	while(k<=j)
	{
    
    
		int mid=(k+j)/2;
		if(ppp(mid))
		{
    
    
			k=mid+1;
			s=mid;
		}
		else j=mid-1;
	}
	printf("%d\n",s);
	
 } 
 
/*
 0 2 3 4 5 删两个石头
 	t=3;
解析
    int i=0; 
  	for(int l=1;l<=n;l++)
	{
	if(a[l]-a[i]>=t) i=l;//下面 
	else s++;
	}
	l=1,a[l]-a[i]=2  s=1(s++);  当s++时,我们就删掉第l个石头 
	l=2,a[l]-a[i]=3  此时的3我们认为是我们已经删去坐标为 2 的石头,和坐标为2的石头的前一个石头的距离 
 
 
 */

おすすめ

転載: blog.csdn.net/weixin_53623850/article/details/120873734