zoukankan      html  css  js  c++  java
  • HDU 1007 Quoit Design (最近点对 分治法)

    Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded. 
    In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring. 

    Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0. 

    InputThe input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated by N = 0. 
    OutputFor each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places. 
    Sample Input

    2
    0 0
    1 1
    2
    1 1
    1 1
    3
    -1.5 0
    0 0
    0 1.5
    0

    Sample Output0.71

    0.00
    0.75
    分治法求解:首先,把平面分成两部分, 那么最近点对存在的位置有两种。
    (1)点对同时在中线的其中一侧
    (2)点对恰好在中线的两侧
    对于第一种情况,我们可以用递归求解点对的最短距离d(不再赘述)。
    对于第二种情况,情况稍微复杂一点,我们不可能直接枚举在两侧的所有点的距离,这样复杂度O(N2)依然很高。我们可以对枚举的过程优化,
    假设中线的横坐标为x0,那么只用考虑横坐标x在(x0-d,x0+d)上的点就好了,因为两点之间满足横坐标之差在d以内,它们的距离才可能小于d。
    但是只处理到这样,复杂度还是不满足题目要求。我们还能进一步优化,把符合题目要求的点筛选出来,按纵坐标排序,从小往大开始比较。首先
    得满足y1+d<y2,否则y2上(后)面的点就不用比较了。满足条件的点对求距离更新d。
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int maxn=1e5+5;
    int a[maxn];
    struct node
    {
        double x,y;
    }p[maxn];
    double dis(node a,node b)
    {
        return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 
    }
    bool cmpx(node a,node b)
    {
        return a.x<b.x;
    }
    bool cmpy(int a,int b)
    {
        return p[a].y<p[b].y;
    }
    double find(int l,int r)
    {
        if(l+1==r)
            return dis(p[l],p[r]);
        int mid=(l+r)/2;
        double ans=min(find(l,mid),find(mid,r));//递归求解第一种情况下的最小距离d
        int cnt=0;    
        for(int i=l;i<=r;i++)
        {
            if(fabs(p[i].x-p[mid].x)<=ans)//筛选
                a[cnt++]=i;
        }
        sort(a,a+cnt,cmpy);
        for(int i=0;i<cnt-1;i++)
        {
            for(int j=i+1;j<cnt;j++)
            {
                if(p[a[j]].y-p[a[i]].y>=ans)//不满足y1+d<y2,跳出循环
                    break;
                ans=min(ans,dis(p[a[i]],p[a[j]]));
            }
        }
        return ans;
    }
    int main()
    {
        int n;
        while(cin>>n,n)
        {
            for(int i=0;i<n;i++)
                scanf("%lf%lf",&p[i].x,&p[i].y);
            sort(p,p+n,cmpx);//首先对横坐标排好序
            printf("%.2lf
    ",find(0,n-1)/2);
        }
        return 0;
    }


  • 相关阅读:
    Oracle EBS—PL/SQL环境初始化之 fnd_global.apps_initialize
    fnd_profile.value的用法
    FND_MESSAGE 消息提示详解
    FORM触发器执行顺序
    大数据实战精英+架构师班 ④ 期
    .Net Core3.0 WebApi 十五:使用Serilog替换掉Log4j
    .Net Core3.0 WebApi 十四:基于AOP的切面redis缓存
    .Net Core3.0 WebApi 十三:自定义返回Json大小写格式
    .Net Core3.0 WebApi 十二:自定义全局消息返回过滤中间件
    .Net Core3.0 WebApi 十一:基于Log4j的全局异常处理
  • 原文地址:https://www.cnblogs.com/orion7/p/7249759.html
Copyright © 2011-2022 走看看