poj 3258 River Hopscotch

來源:互聯網
上載者:User

標籤:結果   otc   style   mes   原創文章   raw   tle   class   turn   

River Hopscotch  Time Limit:2000MS      Memory Limit:65536KB      64bit IO Format:%lld & %lluSubmit 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 torocks (0 ≤ M ≤ N).

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:  LN, 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 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). 

題意:
一條河長度為 L,河的起點(Start)和終點(End)分別有2塊石頭,S到E的距離就是L。河中有n塊石頭,每塊石頭到S都有唯一的距離問現在要移除m塊石頭(S和E除外),要求移除m塊石頭後,使得那時的最短距離儘可能大,輸出那個最短距離。
---------------------
Calm微笑
來源:CSDN
原文: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.