【poj 3258】River Hopscotch 中文题意&题解&代码

River Hopscotch

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10631 Accepted: 4560

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 ≤ 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: 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

题意:一段河,长度len(len<10^9),左河岸0和右河岸len都有一块石头,除此之外河道中还有n块石头
现在要求去掉m块石头,是的相邻石头间隔的最小值最大

输入:len,n,m,河道中每个石头距离左河岸的距离

题解:每次二分这个最小距离,判断去掉多少块石头能够满足

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long  a,b,c,d,n,m;
long long  arr[100000];
int cmp(long long  aa,long long bb)
{
        return aa<bb;
}
int jud(long long cc)
{
        int sum=0;
        int pre=1;
        for (int i=2;i<n;i++)
        {
                if (arr[i]-arr[pre]<cc)
                {
                        sum++;
                        continue;
                }
                pre=i;
        }
        if (arr[n]-arr[pre]<cc) return 0;
        if (sum<=m)
                return 1;
        return 0;


}

int main()
{
        long long  len;
        scanf("%lld%lld%lld",&len,&n,&m);
        for (int i=1;i<=n;i++)
                scanf("%lld",&arr[i]);
        n+=2;
        arr[n-1]=0;
        arr[n]=len;
        sort(arr+1,arr+1+n,cmp);
        long long l,r,mid,ans;
        l=0;
        r=len+1;
        ans=-1;
        //if (jud(4)==1)
                //cout<<"asdasdasdasdasd"<<endl;
        while(l<=r)
        {
                //cout<<"  l&& r  is "<<l<<"   "<<r<<endl;
                mid=(l+r)/2;
               // cout<<mid<<endl;
                if (jud(mid)==1)
                {
                        l=mid+1;
                        ans=max(ans,mid);
                }
                else r=mid-1;
        }
        cout<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/williamcode/article/details/51033985
今日推荐