Quoit Design
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 52337 Accepted Submission(s): 13806
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
Author
CHEN, Yue
Source
首先我借鉴了一下他人的博客,原文地址http://www.cnblogs.com/ACMDoli/articles/4292029.html
原文也分析了,我也就不分析了,这就是分冶的方法
做这个题的时候,让我充分感受到了C++,C两者效率的差别
一开始我用的C++的标准输入输出流进行输入输出,杭电OJ给我的结果是内存超出了,但是把全部的输入输出改为C的输入输出,内存才占用五分之一不到。。。。
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstdio>
using namespace std;
const double eps = 1e-6;
const long long MAXN = 100010;
const double INF = 1e20;
struct Point
{
double x,y;
};
Point p[MAXN];
Point temp[MAXN];
double Fabs(double x)
{
if(x>0)
return x;
else return -x;
}
double dist(Point x,Point y)
{
return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}
bool cmpxy(Point x,Point y)
{
if(x.x!=y.x) return x.x<y.x;
else return x.y<y.y;
}
bool cmpy(Point x,Point y)
{
return x.y<y.y;
}
double Closest_Pair(int left,int right)
{
double d = INF;
if(left == right)
return d;
if(left+1 == right)
return dist(p[left],p[right]);
int mid = (left+right)/2;
double d1 = Closest_Pair(left,mid);
double d2 = Closest_Pair(mid+1,right);
d = min(d1,d2);
int k = 0;
for(int i = left; i < right; i++)
{
if(Fabs(p[mid].x-p[i].x)<d)
temp[k++] = p[i];
}
sort(temp,temp+k,cmpy);
for(int i = 0; i < k; i++)
{
for(int j = i + 1; j < k && temp[j].y - temp[i].y<d; j++)
{
d = min(dist(temp[j],temp[i]),d);
}
}
return d;
}
int main(int argc,char **argv)
{
int n;
while(scanf("%d",&n)==1)
{
if(n==0)
break;
for(int i = 0; i < n; i++)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
}
sort(p,p+n,cmpxy);
double ret = Closest_Pair(0,n-1)/2;
printf("%.2lf\n",ret);
//cout<<showpoint<<setprecision(2)<<ret<<(ret?"":"0")<<endl;
}
return 0;
}