Quoit Design

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
题意:给定平面上的n个点。求一对最短的点的距离
题解:采用分治的方法,将n个点进行排序对半分,所以最短的点对的距离为min(左边的点对的距离,右边的点对的距离,左右形成的点对的距离)
处理左右的时候,对y坐标进行排序。注意剪枝判断否则会超时
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
int n;
struct lmx{
 double x;
 double y;
}p1[100000],p2[100000];
double _cntmin(double a,double b)
{
 return a<b?a:b;
}
bool cmp1(lmx s,lmx t)
{
 return s.x<t.x;
}
bool cmp2(lmx s,lmx t)
{
 return s.y<t.y;
}
double re(lmx s,lmx t)
{
   return sqrt((s.x-t.x)*(s.x-t.x)+(s.y-t.y)*(s.y-t.y));
}
double print(int l,int r)
{
 int i,j;
 double ans;
 if(l+1==r) return re(p1[l],p1[r]);
 if(l+2==r) return _cntmin(re(p1[l],p1[l+1]),_cntmin(re(p1[l+1],p1[r]),re(p1[l],p1[r])));
 else
 {
  int mid=(l+r)>>1;
  int count=0;
     ans=_cntmin(print(l,mid),print(mid+1,r));
        for(i=l;i<=r;i++)
  {
   if(fabs(p1[i].x-p1[mid].x)<=ans)  p2[count++]=p1[i];
  }
  sort(p2,p2+count,cmp2);
        for(i=0;i<count;i++)
  {
   for(j=i+1;j<count;j++)
   {
    if(p2[j].y-p2[i].y>=ans) break;//这个必须要的,否则会超时
    else
    {
     if(re(p2[j],p2[i])<ans) ans=re(p2[j],p2[i]);
    }
   }
  }
  return ans;
 }
}
int main()
{
 int i;
   while(scanf("%d",&n),n)
   {
    for(i=0;i<n;i++)
    {
     scanf("%lf %lf",&p1[i].x,&p1[i].y);
    }
    sort(p1,p1+n,cmp1);
       printf("%.2lf\n",print(0,n-1)/2);
   }
   return 0;
}
posted @ 2013-09-11 13:47  forevermemory  阅读(167)  评论(0编辑  收藏  举报