HDU1007 Quoit Design (平面最近点对)

来源:互联网 发布:淘宝设置发货地址 编辑:程序博客网 时间:2024/05/16 08:19

Quoit Design

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


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

这道题就是经典的平面分治法的使用。
感觉我的代码写的好丑,为什么同样的代码我写出来就是900+ms,别人的就是700+ms啊,伤心……
网上也有很多讲解,我也不罗嗦了。

#include<bits/stdc++.h>using namespace std;const int maxn=1e5+5;const double eps=1e-7;struct note {    double x,y;} a[maxn];int me[maxn];inline double MIN(double min1,double min2) {    return min1<min2?min1:min2;}inline double DIS(struct note aa,struct note bb) {    return sqrt((aa.x-bb.x)*(aa.x-bb.x)+(aa.y-bb.y)*(aa.y-bb.y));}bool cmp_x(struct note aa,struct note bb) {    return aa.x<bb.x;}bool cmp_y(int aa,int bb) {    return a[aa].y<a[bb].y;}double Close_Pair(int l,int r) {    if(l==r-1) {        return DIS(a[l],a[r]);    } else if(l==r-2) {        return MIN(DIS(a[l],a[l+1]),MIN(DIS(a[l],a[l+2]),DIS(a[l+1],a[l+2])));    } else {        int top=0;        int mid=(l+r)>>1;        double tans=MIN(Close_Pair(l,mid),Close_Pair(mid+1,r));        if(tans<=eps)return tans;        for(int i=l; i<=r; ++i) {            if(a[mid].x+tans>=a[i].x&&a[i].x>=a[mid].x-tans) {                me[top++]=i;            }        }        sort(me,me+top,cmp_y);        for(int i=0; i<top; ++i) {            for(int j=i+1; j<top; ++j) {                double td=DIS(a[me[i]],a[me[j]]);                if(td<tans) {                    tans=td;                } else break;            }        }        return tans;    }}int main() {    int n;    while(~scanf("%d",&n)&&n) {        for(int i=0; i<n; ++i) {            scanf("%lf%lf",&a[i].x,&a[i].y);        }        sort(a,a+n,cmp_x);        printf("%.2lf\n",Close_Pair(0,n-1)/2.00);    }    return 0;}
0 0
原创粉丝点击