HDU1007 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

Recommend
JGShining | We have carefully selected several similar problems for you: 1006 1009 1008 1010 1011

题意: 平面最近点对
思路: 看了acdreamers博客写的
用分治思路。
先将平面划为两部分,左部分求一个最近点对距离d1,右平面求一个d2。
取d = min(d1,d2)
取 mid左右x距离d内的点(相当于剪枝了),再在这些点中对应找y距离小于d的点,然后算距离取最小值。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const double INF = 0x3f3f3f3f;
const int maxn = 1e5 + 7;
struct Node
{
    double x,y;
}p[maxn];

int cmp1(Node a,Node b)
{
    if(a.x == b.x)return a.y < b.y;
    return a.x < b.x;
}

int cmp2(int a,int b)
{
    return p[a].y < p[b].y;
}

double dist(Node a,Node b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

int t[maxn];
double dfs(int L,int R)
{
    double d = INF;
    if(L == R)return d;
    if(L + 1 == R)return dist(p[L],p[R]);
    int mid = (L + R) >> 1;
    d = min(d,dfs(L,mid));
    d = min(d,dfs(mid + 1,R));
    int cnt = 0;
    for(int i = L;i <= R;i++)
    {
        if(fabs(p[mid].x - p[i].x) <= d)
        {
            t[++cnt] = i;
        }
    }
    sort(t + 1,t + 1 + cnt,cmp2);
    
    for(int i = 1;i <= cnt;i++)
    {
        for(int j = i + 1;j <= cnt && p[t[j]].y - p[t[i]].y < d ;j++)
        {
            d = min(d,dist(p[t[i]],p[t[j]]));
        }
    }
    return d;
}

int main()
{
    int n;
    while(~scanf("%d",&n) && n)
    {
        for(int i = 1;i <= n;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        sort(p + 1,p + 1 + n,cmp1);
        printf("%.2f\n",dfs(1,n) / 2);
    }
    return 0;
}

还有一种很bug的写法(已经被zqn大佬hack了)
10
1 1
0 2.001
-1 3.002
-2 4.003
-3 5.004
-4 6.005
-5 7.006
-6 8.007
-7 9.008
1 2
这组数据就不对


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const double INF = 0x3f3f3f3f;
const int maxn = 2e5 + 7;
struct Node
{
    double x,y,s;
}p[maxn];

int cmp(Node a,Node b)
{
    return a.s < b.s;
}

double dist(Node a,Node b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

int t[maxn];


int main()
{
    int n;
    while(~scanf("%d",&n) && n)
    {
        for(int i = 1;i <= n;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
            p[i].s = p[i].x + p[i].y;
        }
        sort(p + 1,p + 1 + n,cmp);
        double ans = INF;
        for(int i = 1;i <= n;i++)
        {
            for(int j = i + 1;j <= i + 1 + 6 && j <= n;j++)
            {
                ans = min(ans,dist(p[i],p[j]) / 2);
            }
        }
        printf("%.2f\n",ans);
    }
    return 0;
}


发布了594 篇原创文章 · 获赞 16 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/103743083
今日推荐