程序代码:
Quoit Design1007
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20947 Accepted Submission(s): 5378
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
分析:好像是一道最接近点对问题(其实是英文太差了)
实现:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define fabs(x) (x<0?(-x):x)
#define zero(x) (fabs(x)<1e-6)
typedef struct
{double x;
double y;
int index;
}Point;
Point p[100000],q[100000],temp[100000];
int compx(void const *a,void const *b)
{
double t=((Point *)a)->x-((Point *)b)->x;
if(t>0) return 1;
else if(zero(t)) return 0;
else return -1;
}
int compy(void const *a,void const *b)
{
double t=((Point *)a)->y-((Point *)b)->y;
if(t>0) return 1;
else if(zero(t)) return 0;
else return -1;
}
double dis(Point a,Point b)
{
return hypot(a.x-b.x,a.y-b.y);
}
void Merge(Point a[],int m,Point b[],int n,Point r[])//按y轴顺序合并
{
int i=0,j=0,k=0;
while(i<m&&j<n)
{
if(a[i].y<b[j].y) r[k++]=a[i++];
else r[k++]=b[j++];
}
while(i<m)
r[k++]=a[i++];
while(j<n)
r[k++]=b[j++];
}
double solve(int left,int right,int n,Point p[],Point q[])
{
int mid;
double min,t,d1,d2,midx;
if(n==2) return dis(p[left],p[right]);
else if(n==3)
{
min=dis(p[left],p[right]);
if((t=dis(p[left],p[left+1]))<min) min=t;
if((t=dis(p[left+1],p[right]))<min) min=t;
return min;
}
else
{
mid=(left+right)/2;
int i,j,m=left,n=mid+1;
for(i=left;i<=right;i++)
if(p[i].index==mid)
{q[m++]=p[i];midx=p[i].x;}
else if(p[i].index<mid)
q[m++]=p[i];
else
q[n++]=p[i];
d1=solve(left,mid,mid-left+1,q,p);
d2=solve(mid+1,right,right-mid,q,p);
min=d1<d2?d1:d2;
Merge(q+left,mid-left+1,q+mid+1,right-mid,p+left);//把q重新合并为p
int k=0;
for(i=left;i<=right;i++)
if(fabs(p[i].x-midx)<min)
temp[k++]=p[i];
for(i=0;i<k-1;i++)
for(j=i+1;j<k&&temp[j].y-temp[i].y<min;j++)
if((t=dis(temp[j],temp[i]))<min) min=t;
return min;
}
}
int main()
{
int i,n;
double ans;
while(scanf("%d",&n)&&n!=0)
{
for(i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
qsort(p,n,sizeof(Point),compx);
for(i=0;i<n;i++)
p[i].index=i;
qsort(p,n,sizeof(Point),compy);
ans=solve(0,n-1,n,p,q);
printf("%.2lf\n",ans/2);
}
return 0;
}
[
本帖最后由 价码问题 于 2013-5-7 13:17 编辑 ]