hdu 1007 Quoit Design 平面上的最近点对 分治法+鸽笼原理

来源:互联网 发布:数据库安全测试方法 编辑:程序博客网 时间:2024/05/20 07:36

题意:给出数万个点,如何让计算机在5秒内找出最近点对的距离/2。


Quoit Design

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


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
 

Author
CHEN, Yue
 

Source
ZJCPC2004





#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])#define mem(a,x)  memset(a,x,sizeof a)#define sqr(x)  ((x)*(x))typedef long long ll;typedef pair<int, int> pii;const int maxn= 100000  ;const double eps= 1e-10  ;int n;struct Point{    double x,y;    bool operator<(const Point c)const    {        return x<c.x;    }}p[maxn+10],pL[maxn+10],pR[maxn+10];bool cmp(Point a,Point b){    return a.y<b.y;}double dis(Point a,Point b){    return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y) );}double cal(int le,int ri){    if(ri-le==1) return dis(p[le],p[ri]);//没有在边界条件时返回正无穷,感觉这样处理更好,因为不知道面有多大    if(ri-le==2)    {        double ret=min(dis(p[le],p[le+1] ),dis(p[le+1],p[le+2]) );        return min(ret,dis(p[le],p[le+2]));    }    int mid=(le+ri)>>1;    double ans=min(cal(le,mid),cal(mid+1,ri));    double t=ans;    double ret=p[mid].x;    int nL=0,nR=0;    for(int i=le;i<=ri;i++) if(ret-t<=p[i].x && p[i].x<=ret+t)    {        if(i<=mid)  pL[nL++]=p[i];        else  pR[nR++]=p[i];    }    sort(pL,pL+nL,cmp);    sort(pR,pR+nR,cmp);    int nex=0;    for0(i,nL)    {        double down=pL[i].y-t,up=pL[i].y+t;        while(nex<nR&&pR[nex].y<down )  nex++;        while(nex<nR&&pR[nex].y<=up)        {            ans=min(ans,dis( pL[i] ,pR[nex] ));            nex++;        }    }    return ans;}int main(){   while(scanf("%d",&n)&&n)   {       for1(i,n)  scanf("%lf%lf",&p[i].x,&p[i].y);        sort(p+1,p+1+n);        double ans=cal(1,n)/2;        if(ans<eps)  puts("0.00");        else printf("%.2f\n",ans);   }   return 0;}



0 0
原创粉丝点击