poj-3258 River Hopscotch

原创 2012年03月28日 12:58:16

题目链接:http://poj.org/problem?id=3258

River Hopscotch
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3829   Accepted: 1649

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 Mrocks (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 ofM rocks.

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.

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).
题意:相当于一个数标轴,以0点位起点,输入终点位置L,0-L中的石子块数n,以及要移走的石子块数m;n块石子的坐标会给出;
求(移走m块石子后的每相邻两块石子距离的最大值)的最小值。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int l,n,m;
	while(cin>>l>>n>>m)
	{
		int i;
		int* rock=new int[n+2];   //动态数组。 n个石子以及起始终点共(n+2)个
		rock[n+1]=l;
		rock[0]=0;
		int min=l,max=l,mid;      //max表示一步的最大距离
		for(i=1;i<=n+1;i++)
		{
			if(i<n+1)
				cin>>rock[i];
			if(rock[i]-rock[i-1]<min)
				min=rock[i]-rock[i-1];     //min:一步的最短距离
		}
		sort(rock,rock+(n+2));   //对石子排序
		while(min<=max)
		{
			mid=(min+max)/2;
			int sum=0;
			int delrock=0;     //清除的石子的数目
			for(i=1;i<=n+1;i++)
			{
				if((sum+=rock[i]-rock[i-1]) <=mid)      //sum来表示一段累计的距离,当sum>mid时,作为一段,sum清零重置
					delrock++;
				else
					sum=0;
			}
			if(delrock<=m)      //清除石子数目不大于m(即使等于m也不一定为最优解),下界需增大;否则上界需减小
				min=mid+1;      //  不是min=mid!
			else
				max=mid-1;
		}
		cout<<min<<endl;
	}
	return 0;
} 



版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 3258 River Hopscotch(二分法,最小值最大化)

River HopscotchDescriptionEvery year the cows hold an event featuring a peculiar version of hopscotc...

POJ - 3258/USACO - Dec06 Silver/NOIP 2015 - 提高组 River Hopscotch 贪心+二分搜索

【问题描述】 每年奶牛们举行一场奇特的活动。他们从左岸出发,一个个小心地跳过河上的石头,最后跳到右岸。左右岸上各有一块石头,分别是起点和终点,而它们之间有N块石头在河上,它们与两岸的两块石头处在同一...

poj 3258 River Hopscotch (二分与贪心)

Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully ...

POJ3258River Hopscotch

POJ 3258: 题意:在一段长为L的距离中(1—L),有N个点,去掉其中的M个,使得每个点之间的最小距离最大(包括第一个点和1点,以及第N个点和L)。 二分加贪心,解题思路:二分最小距离,每次...

POJ 3258 - River Hopscotch

题目大意:题目很长哈,简单来说,数轴上有n个石子,第i个石头的坐标为Di,现在要从0跳到L,每次条都从一个石子跳到相邻的下一个石子。现在FJ允许你移走M个石子,问移走这M个石子后,相邻两个石子距离的最...
  • Freenm
  • Freenm
  • 2017年03月16日 22:27
  • 105

【NOIP2015TGD2T1】poj3258 River Hopscotch

二分答案+贪心
  • sdfzyhx
  • sdfzyhx
  • 2016年07月03日 16:42
  • 137

【POJ】[3258]River Hopscotch

也是一道**化最*值的题 掌握技巧之后这种题还是很简单的思路都是通过二分来查找答案 然后判断这个答案符不符合要求 这样的话便可以不断二分 来缩小答案所在区间大小这一题判断答案是否符合的方式 ...

POJ 3258 River Hopscotch(二分法)

Description Every year the cows hold an event featuring a peculiar version of hopscotch that invo...

POJ 3258 River Hopscotch【二分:最大化最小值】

Language: Default River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions...
  • Puyar_
  • Puyar_
  • 2017年06月27日 15:02
  • 125

POJ3258 River Hopscotch(二分法求最大化最小值)

Link:http://poj.org/problem?id=3258 River Hopscotch Time Limit: 2000MS   Memory ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj-3258 River Hopscotch
举报原因:
原因补充:

(最多只允许输入30个字)