zoukankan      html  css  js  c++  java
  • POJ 3258 River Hopscotch (二分法)

    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 torocks (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 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).
     
    题目大意:一群牛要过河,河中有许多石墩,FJ为了锻炼牛的跳跃力,将其中的m个石墩摘掉,求所有情况中最大的跳跃最小值。
     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 int l,m,n,a[50000+11],i,le,ri,ans;
     5 int f(int x,int y, int m)
     6 {
     7     while(x<=y)
     8     {    
     9         int mid = x+(y-x)/2;
    10         int key=0,num=0;
    11         for(i = 1 ; i <= n+1 ; i++)
    12         {
    13             if((num+=a[i]-a[i-1])<= mid)
    14             {
    15                 key++;
    16             }
    17             else
    18             {
    19                 num=0;
    20             }
    21         }
    22             if(key <= m)
    23             {
    24                 x=mid+1;
    25             }
    26             else
    27             {
    28                 y=mid-1;
    29             }
    30     }
    31     return x;
    32 }
    33 int main()
    34 {
    35     while(scanf("%d %d %d",&l,&n,&m)!=EOF)
    36     {
    37         le=1;
    38         ri=l;
    39         a[0]=0;
    40         a[n+1]=l;
    41         for(i = 1 ; i <= n ; i++)
    42         {
    43             scanf("%d",&a[i]);
    44         }
    45         sort(a,a+n+2);
    46         for(i=1;i<=n+1;i++)
    47         {
    48             le=min(le,a[i]-a[i-1]);
    49         }
    50         ans=f(le,ri,m);
    51         printf("%d
    ",ans);
    52     }
    53 }
    ——将来的你会感谢现在努力的自己。
  • 相关阅读:
    vue--一些预设属性
    vue--vux框架的使用
    vue--vConsole
    vue--音乐播放器
    vue--使用vue-cli构建项目
    vue--实例化对象
    vue--数据显示模版上
    CSS--交互效果
    Git SSH公钥配置
    gradle配置国内镜像
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5710300.html
Copyright © 2011-2022 走看看