Poj__3258 River Hopscotch (二分~最大化最小值)

River Hopscotch
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18067   Accepted: 7544

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

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

Source

USACO 2006 December Silver

                 给你最大距离l ~之中有n块石头~求若拿走m个石头~他们之间的最小距离的最大值是多少~;

                 就是一道赤裸裸的最大化最小值问题~~这里有个问题是~枚举mid的上下边界可以更加精确~应该是(min_石头间距离)到l(那个距离)的枚举区间,

                  加两个数据(10 1 1 6),(10 0 0 )答案都应该是10~;

                  具体的细节在代码上叙述;

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int nu[60000];
int n,m;
int check(int mid)
{
    int cnt=0;//用来计数至少要移走几个石头
    int su=0;//su用来判断当前的石头间距离
    int before=0;//计算当前距离时代表左边石头的位置
    for(int s=1;s<=n+1;s++)
    {
        su=(nu[s]-before);//计算距离
        if(su<mid)//如果距离小于我们预定的最小值的话~说明要移走这一块石头,这样当前距离的左边石头位置不变
        {
            cnt++;
        }
        if(su>=mid)//如果大于我们的最小值~就赶紧进行下一轮的匹配~来让移动的石头尽量小
        {
            su=0;
            before=nu[s];
        }
    }
    if(cnt>m)如果最少需要移动的石头大于m~就失败了。
    {
        return 0;
    }
    return 1;
}
int main()
{
    int en;
    scanf("%d%d%d",&en,&n,&m);
    for(int s=1;s<=n;s++)
    {
        scanf("%d",&nu[s]);
    }
    nu[0]=0;
    nu[n+1]=en;
    sort(nu,nu+n+2);//他不是按照顺序个给予的值~所以要排序
    int st=0,ans;//定下枚举的上下区间
    while(st<=en)
    {
        int mid=(en+st)/2;
        if(check(mid))//如果这个的最小值符合~那么记录~并且看看比他更大的还有没有
        {
            ans=mid;
            st=mid+1;
        }
        else//如果这个最小值不成立~就找比他小的
        {
            en=mid-1;
        }
    }
    cout<<ans<<endl;
    return 0;
}



猜你喜欢

转载自blog.csdn.net/chenshibo17/article/details/80157054