• 2020牛客寒假算法基础集训营5 牛牛战队的比赛地


    https://ac.nowcoder.com/acm/contest/3006/B

    题意

      由于牛牛战队经常要外出比赛,因此在全国各地建立了很多训练基地,每一个基地都有一个坐标(x,y)

      这周末,牛牛队又要出去比赛了,各个比赛的赛点都在 轴上。牛牛战队为了方便比赛,想找一个到达训练基地最大距离最小的地方作为比赛地。

    代码

    二分

      对于如何判断当前的这个最大距离是不是一个可行解,可以求当前最大距离对于每个点所确定的 x 的取值范围是否有交点(圆的方程)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=100005;
    struct node
    {
        int x,y;
    }point[maxn];
    int n;
    int check(double r);
    int main()
    {
        int i;
        double l=0,r=0x7fffffff,mid,ans;
    
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%d%d",&point[i].x,&point[i].y);
        
        while(r-l>1e-8)
        {
            mid=(l+r)/2;
            if(check(mid)) ans=mid,r=mid;
            else l=mid;
        }
        printf("%lf",ans);
        system("pause");
        return 0;
    }
    int check(double R)
    {
        double l=-(0x7fffffff),r=0x7fffffff,gap;
        for(int i=1;i<=n;i++)
        {
            if(R<fabs(point[i].y))
                return 0;
            gap=sqrt(R*R-point[i].y*point[i].y);
            l=max(l,point[i].x-gap);
            r=min(r,point[i].x+gap);
        }
        return r>=l;
    }

    三分

      模板。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=100005;
    struct node
    {
        int x,y;
    }point[maxn];
    int n;
    double check(double r);
    int main()
    {
        int i;
        double l=-(0x7fffffff),r=0x7fffffff,mid,mmid;
    
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%d%d",&point[i].x,&point[i].y);
        
        for(i=0;i<100;i++)
        {
          mid=(r+l)/2;
          mmid=(r+mid)/2;
          if(check(mid)>check(mmid))
            l=mid;
          else 
            r=mmid;
        }
        printf("%lf",check(mid));
        system("pause");
        return 0;
    }
    double check(double R)
    {
        double maxn=0;
        for(int i=1;i<=n;i++)
          maxn=max(maxn,sqrt(point[i].y*point[i].y+(point[i].x-R)*(point[i].x-R)));
        return maxn;
    }
  • 相关阅读:
    LeetCode 516. Longest Palindromic Subsequence
    LeetCode 432. All O`one Data Structure
    LeetCode 450. Delete Node in a BST
    LeetCode 460. LFU Cache
    LeetCode 341. Flatten Nested List Iterator
    LeetCode 381. Insert Delete GetRandom O(1)
    LeetCode 380. Insert Delete GetRandom O(1)
    LintCode Coins in a Line III
    LintCode Coins in a Line II
    LintCode Coins in a Line
  • 原文地址:https://www.cnblogs.com/VividBinGo/p/12333973.html
Copyright © 2020-2023  润新知