Hdoj 1007 Quoit Design 题解

Problem Description

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.

Input

The 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.

Output

For 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 Output

0.71
0.00
0.75

Author

CHEN, Yue

Source

ZJCPC2004



思路

最小点对算法:

  1. 只有2个点:就返回这2个点的距离
  2. 只有3个点:就返回两两组成中最短的距离
  3. 大于3个点:采用分治,步骤如下:
    1. 根据横坐标x对所有的店进行升序排列
    2. 找出中心线L,将点集划分为左右2部分\(SL,SR\)
    3. 递归分治解决找出\(d = min(dL,dR)\),表示\(SL,SR\)中的最近点对
    4. 将处于\([L-d,L+d]\)中的点按照y值升序排列,不断更新最近点对的距离(如果最近点对的情况是一个在\(SL\),一个在\(SR\)里面,肯定不会超过这个边界)

代码

#include<bits/stdc++.h>
using namespace std;
struct node
{
    double x;
    double y;
}a[100010],b[100010];
bool cmpx(node a, node b)
{
    return a.x < b.x;
}
bool cmpy(node a, node b)
{
    return a.y < b.y;
}
double dis(node a, node b)
{
    return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
}
double binaryCal(int l, int r, node* a)
{
    if(r-l == 1)    //只有2个点的情况
    {
        return dis(a[l], a[r]);
    }
    if(r-l == 2)    //有3个点的情况
    {
        double tmp1 = dis(a[l],a[l+1]);
        double tmp2 = dis(a[l+1],a[r]);
        double tmp3 = dis(a[l],a[r]);
        return min(tmp1, min(tmp2,tmp3));
    }
    int mid = (l+r)/2;
    double min_d = min(binaryCal(l,mid,a), binaryCal(mid+1,r,a));
    double sqrt_min_d = sqrt(min_d);
    int pos = 0;
    for(int i=l;i<=r;i++)
    {
        if(a[i].x < a[mid].x + sqrt_min_d && a[i].x > a[mid].x - sqrt_min_d)
            b[++pos] = a[i];
    }//将位于[L-d,L+d]范围的点保存到b数组里面
    sort(b+1,b+1+pos,cmpy);     //按照y值进行排序
    for(int i=1;i<=pos;i++)
        for(int j=i+1;j<=pos;j++)
        {
            if(b[j].y - b[i].y > sqrt_min_d)
                break;
            min_d = min(min_d,dis(b[i],b[j]));
        }
    return min_d;

}
int main()
{
    int N;
    while(scanf("%d",&N)!=EOF)
    {
        if(N==0)    break;
        for(int i=1;i<=N;i++)
            scanf("%lf%lf",&a[i].x, &a[i].y);
        double ans = 0.0;
        sort(a+1,a+1+N,cmpx);
        ans = binaryCal(1,N,a);
        printf("%.2lf\n",sqrt(ans)/2);  //最后再处理开平方问题
    }
    return 0;
}

原文地址:https://www.cnblogs.com/MartinLwx/p/9757828.html

时间: 2024-10-09 09:17:52

Hdoj 1007 Quoit Design 题解的相关文章

HDOJ 1007 Quoit Design 最近点对

3年前刚接触ACM的时候做的这一题,交了几十次怎么写都过不了......... 今天无意中又看到了这一题,随手一写终于过了..... Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 33151    Accepted Submission(s): 8709 Problem Description Have y

HDU 1007 Quoit Design (分治)

Quoit Design Problem Description 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 ri

hdu 1007 Quoit Design 分治求最近点对

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29344    Accepted Submission(s): 7688 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat

HDU 1007 Quoit Design

Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 130 Accepted Submission(s): 72   Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings a

分治 [HDU 1007] Quoit Design

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 33577    Accepted Submission(s): 8800 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat

HDU 1007 Quoit Design(二分+浮点数精度控制)

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 47104    Accepted Submission(s): 12318 Problem Description Have you ever played quoit in a playground? Quoit is a game in which fla

poj 1007 Quoit Design(分治)

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29694    Accepted Submission(s): 7788 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat

HDU 1007 Quoit Design【计算几何/分治/最近点对】

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 58566    Accepted Submission(s): 15511 Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat

HDU 1007 [Quoit Design] 分治

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 题目大意:N个点,求最近一对点的距离的一半. 关键思想:最傻瓜式的枚举所有点对的距离找最短 O(n^2),会TLE. 分治可以优化成O(nlogn).二分区间,考虑三种情形:点对的两点都在左区间.两点都在右区间.两点一左一右 前两种情况,可以递归地解出来,分别为d1,d2.第三种,可以依据min(d1,d2)收缩成一条带状区域(勾股定理显然). 然后对带中所有点进行处理,此时又可依据min(d