• POJ_3258_River_Hopscotch_[NOIP2015]_(二分,最大化最小值)


    描述


    http://poj.org/problem?id=3258

    给出起点和终点之间的距离L,中间有n个石子,给出第i个石子与起点之间的距离d[i],现在要去掉m个石子(不包括起终点),求距离最近的两个石子(包括起终点)之间距离的最大值.

    River Hopscotch
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 10841   Accepted: 4654

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

    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

    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

    分析


    二分. 

    同 POJ 2456 , 但有一点不同:start与end位置是已经确定好的,只需要在中间n个石子中确定n-m个即可.第一个石子与a[0]即start位置比较,结束之后还要再比较a[n+1]-a[last]>=x是否成立.(也是注意点1)

     1 #include<cstdio>
     2 #include<algorithm>
     3 using std :: sort;
     4 
     5 const int maxn=50005;
     6 int n,l,m;
     7 int a[maxn];
     8 
     9 void init()
    10 {
    11     scanf("%d%d%d",&l,&n,&m);
    12     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    13     sort(a+1,a+n+1);
    14     a[n+1]=l;
    15 }
    16 
    17 bool C(int x)
    18 {
    19     int last=0;
    20     for(int i=1;i<=n-m;i++)
    21     {
    22         int now=last+1;
    23         while(now<=n&&(a[now]-a[last])<x) now++;
    24         if(now>n) return false;
    25         last=now;
    26     }
    27     if(a[n+1]-a[last]<x) return false;
    28     return true;
    29 }
    30 
    31 int bsearch(int x,int y)
    32 {
    33     while(x<y)
    34     {
    35         int m=x+(y-x+1)/2;
    36         if(C(m)) x=m;
    37         else y=m-1;
    38     }
    39     return x;
    40 }
    41 
    42 void solve()
    43 {
    44     int INF=l/(n+1-m);//共n+1-m个线段,最短的线段最多是l/(n+1-m)
    45     int ans=bsearch(1,INF);
    46     printf("%d
    ",ans);
    47 }
    48 
    49 int main()
    50 {
    51     freopen("river.in","r",stdin);
    52     freopen("river.out","w",stdout);
    53     init();
    54     solve();
    55     fclose(stdin);
    56     fclose(stdout);
    57     return 0;
    58 }
    View Code

    注意:

    1.如上所述.

    2.输入数据是无序的,需先用快排进行排序. 

  • 相关阅读:
    Java第三次作业第四题
    Java第三次作业第三题
    Java第三次作业第二题
    Java第三次作业第一题
    具有注册、登陆以及后台管理功能的web开发
    KMP算法
    二叉排序树-插入算法
    算法刷题-1-单链表操作
    最牛X的编码套路
    day03 高级模块
  • 原文地址:https://www.cnblogs.com/Sunnie69/p/5423190.html
Copyright © 2020-2023  润新知