POJ3258⭐⭐⭐

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 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.


每年,这些牛都会举办一个活动,以一种特殊的跳房子游戏为特色,包括小心翼翼地在河中从一块石头跳到另一块石头。激动人心的场面发生在一条笔直的长河上,起点是一块岩石终点是另一块岩石,距离起点L个单位(1≤L≤1e9)。沿起止岩体之间的河道,出现 N(0≤N≤5e4) 个岩体,每个岩体与起止岩体之间的距离为D[i] (0 < D[i] < L)。

在游戏中,每头牛依次从起始岩石开始,并试图到达终点在结束岩石,只跳从岩石到岩石。当然,不那么敏捷的牛永远无法爬到最后一块石头前,最终会掉进河里。

农民约翰为他的牛感到骄傲,每年都会观看这一活动。但随着时间的推移,他厌倦了看着其他农民胆怯的牛跛行地穿过距离太近的岩石。他计划移走几块石头,以增加一头牛到达终点的最短距离(最大化最小值问题)他知道他无法移除开始和结束的岩石,但他计算出他有足够的资源移除最多M块岩石(0≤M≤N)

FJ想知道在开始移走石头* 之前* 他能增加多少最短的距离。帮助农民约翰确定在移除最佳的M块岩石后,牛必须跳的最大可能的最短距离。


Input

Line 1: Three space-separated integers: LN, 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.


第1行:三个用空格分隔的整数:L、N和M
第2...N+1行:每一行包含一个整数,表示某块石头与起始石头的距离。没有两块石头的位置是相同的


Output

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


第1行:一个整数,表示牛在移除M块石头后所能跳的最短距离


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).


在移除任何石头之前,最短的跳跃是从0(开始)到2的跳跃。在移除2和14处的石头后,最短的跳跃距离是4(从17到21或从21到25)。 (注意理解下面的解析)


题目摘要

移去N块中的M块石头,使得石头之间的最小距离最大,输出这个最大化的最小距离。

解题思路

1.我们可以二分这个最小的距离(假设为m)【如果m很小,那么需要移去的石头很少;如果m很,那么需要移去的石头可能会比较多(相对来说),所以我们可以通过二分,找到这个恰好的、需要移去M块石头的最大值m】

2.那么我们如何写判断条件 int P(int m) 以便缩小范围?【注意看下方注释】

int P(int m)
{
	int num_removed=0;    //记录需要移走的石头个数
	int last_position=0;    //记录上一块(未被移走)石头的位置
	for(int i=1;i<=N;i++)    //遍历石头的距离列表
	{
		if((D[i]-last_position)<m)    //如果两个石头间的距离<m,则移去两个石头间靠右的石头
			num_removed++; 
		else
			last_position=D[i];	    //自行理解,好理解
	}
	return (num_removed<=M&&L-last_position>=m);    //满足这个条件说明m小了
} 

重在理解哈!有问题请提问!

上代码

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 5e4+10;

int L,N,M;
int D[MAXN];

int P(int m)
{
	int num_removed=0;
	int last_position=0;
	for(int i=1;i<=N;i++)
	{
		if((D[i]-last_position)<m)
			num_removed++;
		else
			last_position=D[i];	
	}
	return (num_removed<=M&&L-last_position>=m);
} 

int main()
{
	scanf("%d%d%d",&L,&N,&M);
	for(int i=1;i<=N;i++)
	{
		scanf("%d",&D[i]);
	}
	sort(D+1,D+1+N);
	
	int l=0;
	int r=L+1;
	int mid;
	while(l+1!=r)
	{
		mid=(l+r)/2;
		if(P(mid))
			l=mid;
		else
			r=mid;	
	}
	printf("%d",l);
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值