Quoit Design
Time Limit: 10000/5000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
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
Reference Code
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
const int maxn=1e6+3;
using namespace std;
struct point{
double x,y;
double operator-(const point &oth)const{
double dx=x-oth.x,dy=y-oth.y;
return sqrt(dx*dx+dy*dy);
}
}p[maxn];
bool cmpx(const point &a,const point &b){
return a.x==b.x?a.y<b.y:a.x<b.x;
}
bool cmpy(const point &a,const point &b){
return a.y==b.y?a.x<b.x:a.y<b.y;
}
int main(){
int n;
double res;
while(~scanf("%d",&n)&&n){
res=INF;
for(int i=1;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
sort(p+1,p+n,cmpx);
for (int i=2;i<=n;i++)
res=min(p[i-1]-p[i],res);
sort(p+1,p+n,cmpy);
for (int i=2;i<=n;i++)
res=min(p[i-1]-p[i],res);
printf("%.2lf\n",res/2);
}
}
Tip
1.按照《算法导论》P610中提供的寻找最近点对的算法,此题应该用分治法解决。然而我这么做超时了,不知道问什么,可能是因为在考虑左边区间的点与右边区间的点的连线时没有做合理且必要的剪枝,后来还写出了一堆bug,只好先放弃了这个方法。
2.最后是用了在Discuss中看到的解法,即按横、纵坐标两次排序,分别求两种情况下相邻点对的距离最小值。AC之后,通过构造、尝试举反例等方式并未发现此算法正确性有何问题,因而考虑将这一算法作为一个模板。而且,这个算法的时间复杂度也不高,仅
O
(
n
log
n
)
O(n\log n)
O(nlogn)。