POj 3258 River Hopscotch 二分搜索 最大化最小值

来源:互联网 发布:i5处理器编程够用么 编辑:程序博客网 时间:2024/06/05 18:00
River Hopscotch
Time Limit: 2000MSMemory Limit: 65536KTotal Submissions: 9970Accepted: 4277

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.

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 2214112117

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

Source

USACO 2006 December Silver
错因分析:没有好好理解最大化平均值的思想
分析:本题是个非常典型的二分搜索处理的最大化最小值问题;
            模型:YES YES YES NO NO NO ,要求的是最大的YES,也就是最大化的最小值。理解模型非常关键
假设取一个无穷小的mid,则任意两个石块之间的距离都是大于它的,所以该种情况是能够在条件下(删除石块
数量<=m)的完成的,也就是代表YES,但是取一个无穷大的数时,则是不能在条件(删除石块数量<=m)下完成的
,也就是代表NO,问题查找最大的最小值(最小值就是YES的位置)就成了查找最右边的YES的位置,
            核心思想:用二分搜索枚举最短距离的值,然后再判断在移除石块的数量不超过规定数量的情况下,
能否满足任意两块石块的距离都<=二分枚举的该值,如果能,说明该值是<=最大化的最小距离的,则将该值
作为下限即l=mid,如果不能,则将该值mid减1后作为上限,意最大化的最小值最多只能取到mid-1,即r=mid-1;
           技巧处理: mid=l+(r-l+1)/2;本题中之所以要加1,是因为当出现了一种特殊情况,也就是YES NO时
l=YES的位置,r=NO的位置,如果写成mid=l+(r-l)/2,那么mid值仍为l,l代入ok(mid)后,l=mid,则l的值并没有
改变,则进入了死循环.

#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
int x[50005],L,n,m;;
int ok(int d)
{
int cnt=0,last=0;
for(int i=1;i<=n;i++)
if(x[i]-last<d)
{
cnt++;
if(cnt>m)
return 0;
}
else
last=x[i];
return 1;
}
int main()
{
while(~scanf("%d %d %d",&L,&n,&m))
{
for(int i=1;i<=n;i++)
scanf("%d",&x[i]);
n++;x[n]=L;x[0]=0;
sort(x+1,x+n+1);
int mid,l=0,r=x[n];
while(l<r)
{
mid=l+(r-l+1)/2;
if(ok(mid))
l=mid;
else
r=mid-1;
}
printf("%d\n",r);
}
return 0;
}


第二种做法是将区间写成左闭右开的形式即[l,r),不过要注意的是因为右边开区间无法取到最右边的值
所以r的初始值应大于x[n],可以设为r=x[n]+1,不然会wa

int mid,l=0,r=x[n]+1;
while(r-l>1)
{
mid=(l+r)/2;
if(ok(mid))
l=mid;
else
r=mid;
}
printf("%d\n",l);


阅读全文
0 0
原创粉丝点击