您的位置:首页 > 其它

HDU1007(解题报告)

2014-03-19 17:19 253 查看

Quoit Design

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 27421 Accepted Submission(s): 7237



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和y代表坐标。求两个最近点距离的一半。

解题思想:
  采用分治的办法,用p[i].x和p[i].y分别存i点的横纵坐标,输入完成后对横坐标进行排序;
此时进入find函数,find函数的功能是找到最近点间的距离。如果只有两个点的话,直接算出
两点间距离即可; 如果三个点的话,通过min函数两两比较,也可以求出最下距离;
  But·····奇葩的数据总会有的,如果多于三个点,则用到分治思想。通过mid把p数组分成两
部分,即L到mid、mid+1到r,此处l和r分别为数组p的左右边界。再用find自身递归分别求出
左右两部分的最小距离,用min函数取得左右距离中最小的距离存入ans中。
    接下来考虑最近点对横跨x=mid这种情况,遍历p数组。从x=mid起,保留横坐标在
[min-ans,mid+ans]内的点,并且把该点下标存入a数组,并用cnt记录存入该区间内的点的个数。
a数组存好后,调用cmpy函数,对a数组里的点纵坐标y进行排序。
   最后在以上特定区域内两层循环遍历,使每一个点都和另外每个点距离作比较。如果两个点y轴
坐标的距离超过ans,则break,进行下一次比较,否则不断更新ans。

解题代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

struct node
{
    double x,y;
}p[100005];

int a[100005];

double cmpx(node a,node b)
{
    return a.x<b.x;
}

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

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

double min(double a,double b)
{
    return a<b?a:b;
}

double find(int l,int r)
{
    if(r==l+1)
        return dis(p[l],p[r]);
    if(r==l+2)
        return min( dis(p[l],p[r]), min( dis(p[l+1],p[r]), dis(p[l],p[l+1]) ) );
    int mid = (l+r)/2;
    double ans = min( find(l,mid), find(mid+1,r) );
    int i,j,cnt=0;
    for(i=l;i<=r;i++)
    {
        if( p[i].x>=p[mid].x - ans && p[i].x<=p[mid].x + ans )
            a[cnt++]=i;
    }
    sort(a,a+cnt,cmpy);
    for(i=0;i<cnt;i++)
    {
        for(j=i+1;j<cnt;j++)
        {
            if(p[a[j]].y - p[a[i]].y >= ans)
                break;
            ans = min( dis(p[a[j]], p[a[i]]), ans );
        }
    }
    return ans;
}

int main()
{
    int n;
    //freopen("in.txt","r",stdin);
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        sort(p,p+n,cmpx);
        printf("%.2lf\n",find(0,n-1)/2);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: