1. 程式人生 > >Quoit Design-最近點對-歸併排序

Quoit Design-最近點對-歸併排序

HDU-1007

Quoit Design

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


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

在網上看到的《程式設計之美》中的圖片解釋的很詳細,借鑑一下





#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
struct Node
{double x,y;}q[100005];

double min(double a,double b)
{return a<b? a:b;}

bool cmp_x(Node a,Node b)
{return a.x<b.x;}

bool cmp_y(int a,int b)
{return q[a].y<q[b].y;}

double cal(Node a,Node b)
{
	double x=a.x-b.x;
	double y=a.y-b.y;
	return sqrt(x*x+y*y);
}
int s[100005];
double solve(int l,int r)
{
	double ans=0;
	if(r-l<=2)
	{
		if(l==r) return ans;	
		ans=cal(q[l],q[l+1]);
		if(l+1==r) return ans;
		ans=min(ans,cal(q[l],q[l+2]));
		ans=min(ans,cal(q[l+1],q[l+2]));
		return ans;
	}
	int m=(l+r)>>1,k=0;
	double ans1=solve(l,m);
	double ans2=solve(m+1,r);
	ans=min(ans1,ans2);
	
	for(int i=m-1;i>=l&&(q[m].x-q[i].x)<=ans;i--)
	s[k++]=i;
	for(int i=m+1;i<=r&&(q[i].x-q[m].x)<=ans;i++)
	s[k++]=i;
	sort(s,s+k,cmp_y);
	for(int i=0;i<k;i++)
	for(int j=i+1;j<k&&j<=i+7;j++)
	ans=min(ans,cal(q[s[i]],q[s[j]]));
	return ans;
}
int main()
{
	int n;
    while(cin>>n&&n)
    {
    	for(int i=0;i<n;i++)
    	cin>>q[i].x>>q[i].y;
    	sort(q,q+n,cmp_x);
    	printf("%.2lf\n",solve(0,n-1)/2.0);
	}
}