zoukankan      html  css  js  c++  java
  • BZOJ1650: [Usaco2006 Dec]River Hopscotch 跳石子

    【传送门:BZOJ1650


    简要题意:

      奶牛们喜欢参加一种特别的运动——跳石头。它们分别在一条小河的两岸设置了起点和终点,各 放了一块石头,起点和终点间的跨度有L 米。然后又在河中间放置了N 块石头,这些石头和起点终 点处于同一条直线上,第i 块石头距离起点有Di 米。 游戏的时候,奶牛从起点出发,依次跳过每块石头,最后达到终点。如果两块相邻石头之间的距 离太大,有些奶牛就会跳不过去,掉到河里。约翰打算使坏,他想偷偷抽掉其中M 块石头,使得剩 余的石头之间最近的距离最大。他不能拿走终点和起点处的石头。请问他该拿走哪些石头,才能让剩 下的石头的最短距离最长?


    输入格式:

      • 第一行:三个整数L,N 和M,1 ≤ L ≤ 10^9, 0 ≤ M ≤ N ≤ 50000

      • 第二行到第N + 1 行:第i + 1 行有一个整数Di,0 < Di < L


    输出格式:

      • 单个整数:表示最短距离的最大值


    样例输入:

    25 5 2

    2

    14

    11

    21

    17


    样例输出:

    4


    样例解释:

      移除任何石头之前,最近的距离是起点到2,移走2和14之后,最近的距离是从17到21


    题解:

      二分距离,一块一块石头判断,要用栈来维护,因为起点终点的石头不能动,终点有可能和其他石头靠太近


    参考代码:

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    int a[51000];int L,n,m;
    int sta[51000];int tp;
    bool check(int x)
    {
        memset(sta,0,sizeof(sta));
        int tp=0;
        int mm=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]-sta[tp]<x)
            {
                mm++;
                if(mm>m) return false;
            }
            else sta[++tp]=a[i];
        }
        while(L-sta[tp]<x)
        {
            tp--;
            mm++;if(mm>m) return false;
            if(tp==0) return false;
        }
        return true;
    }
    int main()
    {
        scanf("%d%d%d",&L,&n,&m);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        int l=0,r=1000000000;
        int ans=0;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(check(mid)==true)
            {
                ans=mid;
                l=mid+1;
            }
            else r=mid-1;
        }
        printf("%d
    ",ans);
        return 0;
    }

     

  • 相关阅读:
    JLOI2014 聪明的燕姿【搜索-细节】
    JOI2014 バス通学【思维】
    【线段树】USACO 08FEB Hotel G
    USACO20FEB Equilateral Triangles P【思维-曼哈顿距离-切比雪夫距离】
    USACO 2019DEC Milk Visits
    【规律】ABC179 E Sequence Sum
    【前缀和优化dp】ABC179 D Leaping Tak
    SPOJ2916 GSS5-Can you answer these queries V【线段树】
    SPOJ1557 GSS2-Can you answer these queries II【线段树】
    推荐系统之余弦相似度的Spark实现
  • 原文地址:https://www.cnblogs.com/Never-mind/p/7765131.html
Copyright © 2011-2022 走看看