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 ( ≤ L ≤ ,,,). Along the river between the starting and ending rocks, N ( ≤ N ≤ ,) more rocks appear, each at an integral distance Di from the start ( < 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 ( ≤ 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 : Three space-separated integers: L, N, and M
Lines ..N+: 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 : A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks

Sample Input


Sample Output


Hint

Before removing any rocks, the shortest jump was a jump of  from  (the start) to . After removing the rocks at  and , the shortest required jump is a jump of  (from  to  or from  to ).

Source

 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
#define N 50006
int dis[N];
int L,n,m;
bool solve(int min_dis){
int last=;
int cnt=;
for(int i=;i<=n+;i++){
if(dis[i]-dis[last]<=min_dis) cnt++;
else last=i;
} if(cnt>m) return true;
return false; }
int main()
{ while(scanf("%d%d%d",&L,&n,&m)==){
for(int i=;i<=n;i++){
scanf("%d",&dis[i]);
} if(n==m){
printf("%d\n",L);
continue;
} dis[]=;
dis[n+]=L;
sort(dis+,dis+n+);
int low=;
int high=L;
while(low<high){
int mid=(low+high)>>;
if(solve(mid)){
high=mid;
}
else{
low=mid+;
}
}
printf("%d\n",low);
}
return ;
}
上一篇:ArryList vs LinkedList


下一篇:SystemVerilog语言简介(一)