1. 程式人生 > >poj 3258 River Hopscotch

poj 3258 River Hopscotch

nes sin span scrip 起點 sum template emp sco

River Hopscotch

Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status

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 enanswerng rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral answerstance answer from the start (0 < answer < L).

To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the enanswerng rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, enanswerng 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 answerstances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest answerstance a cow will have to jump to reach the end. He knows he cannot remove the starting and enanswerng rocks, but he calculates that he has enough resources to remove up toM

rocks (0 ≤ MN).

FJ wants to know exactly how much he can increase the shortest answerstance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest answerstance 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 inanswercating 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 answerstance 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).

題意:
一條河長度為 L,河的起點(Start)和終點(End)分別有2塊石頭,S到E的距離就是L。河中有n塊石頭,每塊石頭到S都有唯一的距離問現在要移除m塊石頭(S和E除外),要求移除m塊石頭後,使得那時的最短距離盡可能大,輸出那個最短距離。
---------------------
作者:Calm微笑
來源:CSDN
原文:https://blog.csdn.net/yao1373446012/article/details/51025699
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

解題思路:
由題意易知,answer一定在區間【0,L】,
那麽我們可以二分查找answer,以為answer間隔依次挑選石頭,數量記為num,
如果num<=m,說明answer選小了,此時令low=mid+1; answer=mid;(answer=mid,是因為間距小於mid的數量num要小於m,
也就是說如果此時的answer為最終答案,那麽一定要移動的石頭數量num小於m,剩余的移動次數可去移動別的,不影響此時的結果,所以令answer=mid

如果num>m,說明answer選大了;此時令high=mid-1;
#include<stdio.h>
#include<algorithm>  
using namespace std;
int n;
int main()
{
    int l,m,i,ans;
    int dist[50000+5];
    while(~scanf("%d%d%d",&l,&n,&m))
    {
        dist[0]=0;
        dist[n+1]=l;
        for(i=1;i<=n;i++)
            scanf("%d",dist+i);
        sort(dist+1,dist+n+1);
        int low=0,high=l;
        while(high-low>=0)
        {
            int num=0,present=0,mid=(low+high)>>1,sum=0;
            for(i=1;i<=n+1;i++)
                if((sum+=dist[i]-dist[i-1])<mid)//兩相鄰石頭間距比 預定最大最小間距mid小,就移除一次石頭
                   num++;
                else
                   sum=0;
            if(num<=m)//移動的次數少於m,即間距小於mid的石頭少於m,說明mid要變大一點
            {
               low=mid+1;
               ans=mid;
            }
            else
                high=mid-1; 
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

poj 3258 River Hopscotch