hdu1007之分治

来源:互联网 发布:网络视频安防监控系统 编辑:程序博客网 时间:2024/06/01 11:09

Quoit Design

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


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
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1009 1008 1010 1011 1024 

题目大意:题目给出n个点,要求输出最近点对的距离的一半。

题目分析:首先肯定是按照x排序,然后将之分成两半求出两边的的最近点对的距离,然后对两边一次递归下去。但是这样会有一个问题,如果最近点对出现在不同的集合怎么办?所以需要比较一下,但是需要比较两个
集合所有的点吗?没必要!!只需比较x坐标与两个集合中间线的距离小于之前求出的最小ans的点,然后对这
些点也没必要全部比较,将这些点按y坐标从小到大排序,如果两点之间的距离大于之前求得的最小ans,直接break。

代码:
#include <cmath>#include <stdio.h>#include <iostream>#include <algorithm>using namespace std;const int maxn=100000+100;struct Point{    double x,y;};Point p[maxn];int num[maxn];bool cmpx(Point a,Point b){    return a.x<b.x;}bool cmpy(int a,int b){    return p[a].y<p[b].y;}double min(double a,double b){    return a<b?a:b;}double dis(Point a,Point b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}double solve(int l,int r){    if(l+1==r){        return dis(p[l],p[r]);    }    if(l+2==r){        return min(dis(p[l],p[l+1]),min(dis(p[l+1],p[r]),dis(p[l],p[r])));    }    int cnt=0;    int mid=(l+r)>>1;    double ans=min(solve(l,mid),solve(mid+1,r));    for(int i=l;i<=r;i++){        if(dis(p[i],p[mid])<=ans){            num[cnt++]=i;        }    }    sort(num,num+cnt,cmpy);    for(int i=0;i<cnt;i++){        for(int j=i+1;j<cnt;j++){            if(dis(p[num[i]],p[num[j]])>=ans){                break;            }            ans=min(ans,dis(p[num[i]],p[num[j]]));        }    }    return ans;}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int n;    while(scanf("%d",&n)!=EOF&&n)    {        for(int i=1;i<=n;i++){            scanf("%lf%lf",&p[i].x,&p[i].y);        }        sort(p+1,p+n+1,cmpx);        printf("%.2lf\n",solve(1,n)*1.0/2);    }    return 0;}


原创粉丝点击