现在的位置: 首页 > 综合 > 正文

HDU 1007 Quoit Design 分治法求最近点对

2018年05月02日 ⁄ 综合 ⁄ 共 2354字 ⁄ 字号 评论关闭

Quoit Design

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

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
/*
HDOJ 1007 分治法求最近点对
分治法主要是分 左集合 右集合,分别求出最小的值d1,d2再合并
合并主要的是有可能最小的点对 一个在左集合,一个在右集合

所以要考虑,首先筛掉横坐标的距离(点x到点mid)就比d1,d2最小的值大的 
然后在选出的点中,挑选距离最小的。
挑选方法同上,这里按y排序, 首先筛掉纵坐标的距离就比d1,d2最小的值还大的; 
*/
#include<iostream>
#include<stdio.h>
#include<cmath>  
#include<algorithm> 
using namespace std;
#define N 100010
struct point{
	double x,y;
};
int n;
point p[N],temp[N];


bool cmpx(point a,point b) {return a.x<b.x;}  
bool cmpy(point a,point b) {return a.y<b.y;} 

double dis(point a,point b)
{
	return sqrt(double((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));
}

double close(int left,int right)
{
	double a,b,c,min;
	int mid,i,j,k; 
	
	if(left+1==right)//相邻 
		return dis(p[left],p[right]);
	else if(left+2==right)//中间隔一个 
	{
		 
        a=dis(p[left],p[left+1]);
        b=dis(p[left+1],p[right]);
        c=dis(p[left],p[right]);
        if(b<a)  a=b;  
        if(c<a)  a=c;  
        return a;  
	}
	
	mid=(left+right)/2;
	a=close(left,mid);
	b=close(mid,right);
	min=a>b?b:a;
	
	for(i=left,k=0;i<=right;i++)
		if(fabs(p[i].x-p[mid].x)<=min)
			temp[k++]=p[i];
	sort(temp,temp+k,cmpy);
	
	for(i=0;i<k;i++)
		for(j=i+1;j<k;j++)
		{
			if(temp[j].y-temp[i].y>=min)
				break;
			a=dis(temp[i],temp[j]);
			if(a<min)
				min=a;
		}
	return min;
}

int main()
{
	int i;
	
//	freopen("test.txt","r",stdin);
	while(scanf("%d",&n),n)
	{
		for(i=0;i<n;i++)
			scanf("%lf%lf",&p[i].x,&p[i].y);
		sort(p,p+n,cmpx);
		printf("%.2lf\n",close(0,n-1)/2);
	}
	
	return 0;
}

抱歉!评论已关闭.