您的位置:首页 > 其它

求点之间的最短距离问题

2018-02-10 14:45 281 查看

Quoit Design

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


[align=left]Problem Description[/align]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.
 
[align=left]Input[/align]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.
 
[align=left]Output[/align]For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.
 
[align=left]Sample Input[/align]
2
0 0
1 1
2
1 1
1 1
3
-1.5 0
0 0
0 1.5

[align=left]Sample Output[/align]
0.71
0.00
0.75#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 100007
int a
;
struct point
{
double x;
double y;
}pt
;
bool cmp_x(point a,point b)
{
if(a.x!=b.x) return a.x<b.x;
else return a.y<b.y;
}
bool cmp_y(int id1,int id2)
{
return pt[id1].y<pt[id2].y;
}
double getDistance(const point &a,const point &b)
{
double x1=a.x-b.x;
double y1=a.y-b.y;
return sqrt(x1*x1+y1*y1);
}
double getResult(int le,int ri)
{
double ans=0;
if(ri-le+1<=3)
{
if(ri-le+1==1) return ans;
ans=getDistance(pt[le],pt[le+1]);
if(ri-le+1==2) return ans;
for(int i=le;i<ri;i++)
{
for(int j=i+1;j<=ri;j++)
{
ans=min(ans,getDistance(pt[i],pt[j]));
}
}
return ans;
}
int middle=(le+ri)/2;
double min1=getResult(le,middle);
double min2=getResult(middle+1,ri);
ans=min(min1,min2);
int k=0;
for(int m=middle-1;m>=le&&pt[middle].x-pt[m].x<=ans;--m) a[k++]=m;
for(int i=middle+1;i<=ri&&pt[i].x-pt[middle].x<=ans;++i) a[k++]=i;
sort(a,a+k,cmp_y);
for(int i=0;i<k;i++)
{
for(int j=i+1;j<k&&j<=i+7;j++)
{
ans=min(ans,getDistance(pt[a[i]],pt[a[j]]));
}
}
return ans;
}

int main()
{
int n;
while(scanf("%d",&n),n)
{
for(int i=0;i<n;i++)
{
scanf("%lf %lf",&pt[i].x,&pt[i].y);
}
sort(pt,pt+n,cmp_x);
printf("%.2lf\n",getResult(0,n-1)/2.0);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: