求点之间的最短距离问题
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
0
[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);
}
}
相关文章推荐
- 最短路径问题平面上有n个点(n<=100),每个点的坐标均在-10000~10000之间。其中的一些点之间有连线。若有连线,则表示可从一个点到达另一个点,即两点间有通路,通路的距离为两点间的直线距离。
- c/c++常用算法(15) -- 经典数据结构(城市之间的最短距离问题)
- 最短编辑距离变形----DNA对比问题
- ACM-ICPC 2015 Asia Tsukuba Regional Online Open Contest B(两挡板之间放着圆球,求挡板最短距离)
- [经典面试题][腾讯]选择原料工厂(最短距离问题)
- 素数最短距离问题
- Floyd算法(求每一对顶点之间的最短距离)
- 最大最短距离问题
- 最短编辑距离问题
- 菜鸟上路 杭电OJ 1007 求平面上两点之间最短距离--分而治之以及关键点的考虑
- 最短编辑距离问题 : Levenshtein Distance
- 两个数之间的最短路径问题
- codeforces 590C C. Three States(bfs+连通块之间的最短距离)
- [经典面试题][腾讯]选择原料工厂(最短距离问题)
- Dijkstra算法求解最短距离问题
- soj1209- 最短的距离(精度问题)
- 一类最短距离问题
- hdu 3790 最短路径问题(距离和费用)
- 两个数之间的最短路径问题
- Python基于Floyd算法求解最短路径距离问题实例详解