hud 1007 Quoit Design(分治求最小点对)

来源:互联网 发布:李铁军 李婧磁 知乎 编辑:程序博客网 时间:2024/05/22 03:10

hud 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
20 01 121 11 13-1.5 00 00 1.50
 

Sample Output
0.710.000.75
 

题目链接:点击打开链接
代码:

#include<cstdio>#include<algorithm>#include<cmath>using namespace std;struct point{    double x;    double y;};point p[100005];point m[100005];int cmp_x(point p1,point p2){    if(p1.x==p2.x)        return p1.x<p2.x;    return p1.y<p2.y;}int cmp_y(point p1,point p2){    return p1.y<p2.y;}double dis(point p1,point p2){    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));}double findMinDis(int s,int e){    if(e-s==1)        return dis(p[e],p[s]);    int mid=(e+s)/2;    double minz=findMinDis(s,mid);    double miny=findMinDis(mid,e);    double mini=min(minz,miny);    int num=0;    for(int i=s;i<=e;i++)    {        if(fabs(p[mid].x-p[i].x)<=mini)            m[num++]=p[i];    }    sort(p,p+num,cmp_y);    for(int i=0;i<num;i++)    {        for(int j=i+1;j<num&&m[j].y-m[i].y<mini;j++)            mini=min(mini,dis(m[i],m[j]));    }    return mini;}int main(){    int n;    while(~scanf("%d",&n))    {        if(n==0)            break;        for(int i=0;i<n;i++)            scanf("%lf %lf",&p[i].x,&p[i].y);        sort(p,p+n,cmp_x);        printf("%.2lf\n",findMinDis(0,n-1)/2);    }    return 0;}

1 0
原创粉丝点击