求最近点对,模板题。。。。采用的是分治的思想。http://blog.youkuaiyun.com/smallacmer/article/details/7763407
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.
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<iostream>
#include<string.h>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define N 100005
using namespace std;
typedef struct
{
double x;
double y;
}Node;
Node s[N],s1[N];
bool cmp(Node a,Node b)
{return a.x<b.x;}
bool cmp1(Node a,Node b)
{return a.y<b.y;}
double distan(Node a,Node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double slove(int le,int ri)
{
if(le+1==ri) return distan(s[le],s[ri]);
if(le+2==ri) return min(min(distan(s[le],s[ri]),distan(s[le+1],s[ri])),distan(s[le],s[le+1]));
int mid=(le+ri)>>1;
double ans=min(slove(le,mid),slove(mid+1,ri));
int cnt=0;
for(int i=le;i<=ri;++i)
if(s[i].x-s[mid].x<=ans||s[mid].x-s[i].x<=ans)
s1[cnt++]=s[i];
sort(s1,s1+cnt,cmp1);
for(int i=0;i<cnt;++i)
for(int j=i+1;j<cnt;++j)
{
if(s1[j].y-s1[i].y>=ans) break;
ans=min(ans,distan(s1[i],s1[j]));
}
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n),n)
{
for(int i=0;i<n;++i) scanf("%lf%lf",&s[i].x,&s[i].y);
sort(s,s+n,cmp);
printf("%.2lf\n",slove(0,n-1)/2);
}return 0;
}

本文探讨了如何通过分治法解决求最近点对的问题,给出了一种有效的算法实现,并提供了完整的AC代码示例。该算法能够精确计算平面上两点之间的最小距离。
1250

被折叠的 条评论
为什么被折叠?



