River Hopscotch

River Hopscotch

 

Every year thecows hold an event featuring a peculiar version of hopscotch that involvescarefully jumping from rock to rock in a river. The excitement takes place on along, straight river with a rock at the start and another rock at theend, Lunits 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 integraldistance 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 atthe ending rock, jumping only from rock to rock. Of course, less agile cowsnever make it to the final rock, ending up instead in the river.

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

FJ wants to knowexactly how much he can increase the shortest distance *before* hestarts removing the rocks. Help Farmer John determine the greatest possibleshortest distance a cow has to jump after removing the optimal set of M rocks.

Input

Line 1: Three space-separated integers: LN,and M 
Lines 2.. N+1: Each line contains a single integer indicating howfar some rock is away from the starting rock. No two rocks share the sameposition.

Output

Line 1: A single integer that is the maximum of theshortest 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 anyrocks, the shortest jump was a jump of 2 from 0 (the start) to 2. Afterremoving the rocks at 2 and 14, the shortest required jump is a jump of 4 (from17 to 21 or from 21 to 25).

一道很好的二分题目

思路:

二分得到一个长度,小于这个长度的边就要删去,判断要删去的石头的个数是否等于m

对于这其中的几点我想详细说一下:

关于删边的问题:众所周知,一条边由两个端点组成,那么要去除这条边只要删去这条边的任一端点就行(这时候就要判断删去哪个更好了,问题变得复杂了,但是我们可以用贪心使这个问题变简单)如图:

四个端点暂且称之为为1,2,3,4号端点,目前2,3端点之间的边长度小于mid,我们选择删除右边的端点(即3号端点)将这个端点邻接的边连起来,按照这个方法,可以保证要删去的2,3端点之间的边左侧的边的长度都是大于mid(即使存在过小于的,也被合并了),而右边的边的长度不确定,若其长度小于mid,将2,3端点之间的边附加到这条边上就可以达到我们的目的是短的边尽可能长

关于结果:当删去石头的个数等于m时,结果不一定是最优解,因为要使最短边尽可能长(要找其上界),所以cnt==m时还要试图增大l

关于l和r选取:选择l=0,r=L和选择l=min_width,r=max_width都行(后者会更快)

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int L,n,m,l=1e9,r=-1;
	int an[50005];
	cin >> L >> n >> m;
	an[0]=0,an[n+1]=L;
	for(int i=1;i<=n+1;i++)
	{
		if(i<=n)
			cin >> an[i];
		r=max(r,an[i]-an[i-1]);
		l=min(l,an[i]-an[i-1]);			
	}		
	sort(an+1,an+n+1);
	
	while(l<=r)
	{
		int mid=(l+r)/2;
		int front=0,cnt=0;
		for(int i=1;i<=n+1;i++)
		{
			if(mid>=an[i]-an[front])
				cnt++;
			else
				front=i;
		}
		if(cnt>m)
			r=mid-1;
		else
			l=mid+1;
	}
	
	cout << l ;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值