###### wchhlbt的博客

towards world final~

###### POJ 3258 River Hopscotch （二分法）

River Hopscotch
 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10889 Accepted: 4677

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

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

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int a[50005];
int vis[50005];

int main()
{
//freopen("test.txt","r",stdin);
int l,m,n,i;
int up,low,mid;
int ans;
scanf("%d%d%d",&l,&n,&m);
a[0] = 0;
a[n+1] = l;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n+1);
low = 0;up = l + 1;mid = (low + up)/2;//特别注意这里要把上界声明为l+1，因为l本身可能是答案，例如数据1 0 0
while(up-low>1)
{
//memset(vis,0,sizeof(vis));
//ans = mid;
int last = a[0];
int k = 0;
int flag = 0;
for(i=1;i<=n;i++)
{
if(k>m)
{
flag = 1;
break;
}
else if(a[i]-last<mid)
{
k++;
}
else
{
last = a[i];
}
}
if(k>m)     flag = 1;
if(a[n+1]-last<mid && k==m)     flag = 1;
if(flag)    up = mid;
else        low = mid;
mid = (low+up)/2;
}
printf("%d\n",low);
//cout << "Hello world!" << endl;
return 0;
}

#### 【二分法】POJ3258-River Hopscotch

2016-07-27 14:53:18

#### POJ - 3258 River Hopscotch 二分

2015-05-11 18:22:04

#### POJ 3258 River Hopscotch （二分经典）

2016-06-03 07:33:20

#### poj River Hopscotch 3258 （二分）好题

2015-12-20 22:17:49

#### POJ 3258 River Hopscotch（二分）

2015-07-25 14:34:53

#### POJ3258：River Hopscotch(二分)

2014-08-18 14:45:42

#### usaco 2006 Dec【River Hopscotch跳石头】

2016-10-31 21:40:07

#### poj3258River Hopscotch

2016-07-27 20:25:42

#### POJ 3258：River Hopscotch 二分的好想法

2015-08-31 18:52:36

#### poj3258 二分及一些思考

2012-03-18 14:50:12

## 不良信息举报

POJ 3258 River Hopscotch （二分法）