HDU1007——Quoit Design(最近点问题)

Quoit Design

今天从这里开始^_^

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 46707    Accepted Submission(s): 12187


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
 
 
题意:
        给你N个点,求出这些点中以最近的两个点为直径的圆的半径。(有点绕..)
 
解题思路:(看着有点长长的....)
       先把这N个点按x轴排序,找到中间位置,再把分开的区域找中间位置,直到一个区域内最多三个点,每个大区域都有左右区域(这时每个区域最多三个点),然后取最小线段ans=min(左最短,右最短),这时你就会有一个疑问,如果左右区域之间有连线比ans短呢? 那咱们就来探讨一下,假如中线为m,且左右最短线为ans,如果想在中间找到比ans短的线,那么它的范围应该怎么确定,显然是[m-ans,m+ans],这是x轴的范围,咱们先把这个区域内的点存到一个数组里,然后再把他们按y轴排序,这时你会想,不会把他们每个点之间的距离都求出来比吧,当然.....会的,不过需要确定一个范围,你总不能把y=0的点与y=2*ans的点比吧,那肯定比ans长,所以这里的范围就确定到一个ans以内,这时就形成了一个矩形范围,求长2*ans宽ans内的点的最小距离,不过这个看似普通的矩形还有一个邪门的定理,说是这个矩形里面最多有六个点,老夫也不是很懂,不过贫僧知道《算法导论》里有这个相关知识,贫道就不在这里多言了,你只需要把这其中几个点之间的距离求出来再与ans比出最小的,那么这个大区域的最小值就解决了,有没有感觉感觉很乱...别担心,还有更大的区域等着你(递归回去就可以),就像那句名言:假如算法欺骗了你,不要忧伤,不要心急,因为算法明天还会再来欺骗你~微笑
 
代码实现:

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

using namespace std;
#define maxn 100010

struct node
{
    double x;
    double y;
}p[maxn];
int a[maxn];
int n;

bool cmp ( node a, node b )
{
    if( a.x!=b.x )
        return a.x<b.x;
    else return a.y<b.y;
}

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

double getdis( const node &a, const node &b )
{
    double X = a.x - b.x;
    double Y = a.y - b.y;
    return sqrt(X*X + Y*Y);
}

double solve(int l, int r)
{
    double ans = 0;
    if( r-l < 3 )
    {
        if( r-l == 0 )    return ans;
        ans = getdis( p[l], p[l+1]);
        if( r-l == 1 )    return ans;
        for( int i=l; i<r; ++i )
        {
            for( int j=i+1; j<=r; ++j )
            {
                ans = min(ans, getdis(p[i], p[j]));
            }
        }
        return ans;
    }
    int m = (r+l) >> 1;
    double dis1 = solve(l, m);
    double dis2 = solve(m+1, r);
    ans = min(dis1, dis2);
    int k=0;
    for( int i=m-1; i>=l && p[m].x-p[i].x<=ans; --i ) a[k++] = i;
    for( int i=m+1; i<=r && p[i].x-p[m].x<=ans; ++i ) a[k++] = i;
    sort(a, a+k, cmy);
    for( int i=0; i<k; ++i )
    {
        for( int j=i+1; j<k&&j<=i+7; ++j )
        {
            ans = min(ans, getdis(p[a[i]], p[a[j]]));
        }
    }
    return ans;
}

int main()
{
    while( ~scanf("%d",&n) )
    {
        if( !n ) break;
        for( int i=0; i<n; ++i )  scanf("%lf %lf",&p[i].x, &p[i].y);
        sort(p, p+n, cmp);
        printf("%.2lf\n",solve(0, n-1)/2.0);
    }
    return 0;
}


 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值