hdu1007 分治

来源:互联网 发布:串口检测软件 编辑:程序博客网 时间:2024/05/17 03:06

http://acm.hdu.edu.cn/showproblem.php?pid=1007



Quoit Design

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


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
 

先说下题意,很简单,给n个点的坐标,求距离最近的一对点之间距离的一半。

第一行是一个数n表示有n个点,接下来n行是n个点的x坐标和y坐标。实数。



#include <iostream>#include <stdio.h>#include <algorithm>#include <cmath>#define MAX 100005using namespace std;struct node{    double x,y;};node point[MAX],px[MAX],py[MAX];bool cmpx(node a,node b){    return a.x<b.x;}bool cmpy(node a,node b){    return a.y<b.y;}double d(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 closeset(int s,int e){    if(s+1==e)///当只有一条边时        return d(px[s],px[e]);    if(s+2==e)///当只有两条边时        return min(d(px[s],px[e]),min(d(px[s+1],px[s]),d(px[s+1],px[e])));    int mid=(s+e)/2;    double  ans=min(closeset(s,mid),closeset(mid+1,e));///递归求解,一直减小范围    int cnt=0;    for(int i=s;i<=e;i++)    {        if(px[i].x>=px[mid].x-ans&&px[i].x<=px[mid].x+ans)            py[cnt++]=px[i];    }    sort(py,py+cnt,cmpy);    int i,j;    for( i=0; i<cnt; i++)    {        for( j=i+1; j<cnt; j++)        {            if(py[j].y-py[i].y>=ans)                break;            ans=min(ans,d(py[i],py[j]));        }    }    return ans;}int main(){    int t;    while(scanf("%d",&t)!=EOF)    {        if(t==0)            break;        for(int i=0; i<t; i++)        {            scanf("%lf%lf",&point[i].x,&point[i].y);            px[i].x=point[i].x;            px[i].y=point[i].y;        }        sort(px,px+t,cmpx);        double ans=closeset(0,t-1);        printf("%.2lf\n",ans/2.0) ;    }    return 0;}


1 0
原创粉丝点击