Quoit Design
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 54103 Accepted Submission(s): 14249
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
Recommend
解题思路:分治,同时附上求最远点对的代码(凸包+旋转卡壳)
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define LL long long
const double INF=1e20;
struct Point
{
double x,y;
Point(double _x=0,double _y=0)
{
x=_x;
y=_y;
}
Point operator-(const Point &b)const
{
return Point(x-b.x,y-b.y);
}
double operator^(const Point &b)const
{
return x*b.y-y*b.x;
}
double operator*(const Point &b)const
{
return x*b.x+y*b.y;
}
void input()
{
scanf("%lf %lf",&x,&y);
}
};
double dist2(Point a,Point b)
{
return (a-b)*(a-b);
}
const int MAXN=500100;
Point List[MAXN];
int Stack[MAXN],top;
bool _cmp(Point p1,Point p2)
{
int tmp=(p1-List[0])^(p2-List[0]);
if(tmp>0) return true;
else if(tmp==0&&dist2(p1,List[0])<=dist2(p2,List[0])) return true;
else return false;
}
void Graham(int n)
{
Point p0;
int k=0;
p0=List[0];
for(int i=1; i<n; i++)
if(p0.y>List[i].y||(p0.y==List[i].y&&p0.x>List[i].x))
{
p0=List[i];
k=i;
}
swap(List[k],List[0]);
sort(List+1,List+n,_cmp);
if(n==0)
{
top=1;
Stack[0]=0;
return ;
}
if(n==2)
{
top=2;
Stack[0]=0,Stack[1]=1;
return ;
}
Stack[0]=0,Stack[1]=1;
top=2;
for(int i=2; i<n; i++)
{
while(top>1&&((List[Stack[top-1]]-List[Stack[top-2]])^(List[i]-List[Stack[top-2]]))<=0) top--;
Stack[top++]=i;
}
}
double rotating_calipers(Point p[],int n)
{
double ans=0;
Point v;
int cur=1;
for(int i=0; i<n; i++)
{
v=p[i]-p[(i+1)%n];
while((v^(p[(cur+1)%n]-p[cur]))<0)
cur=(cur+1)%n;
ans=max(ans,max(dist2(p[i],p[cur]),dist2(p[(i+1)%n],p[(cur+1)%n])));
}
return ans;
}
Point p[MAXN];
int tmpt[MAXN];
bool cmpxy(const Point& a, const Point& b)
{
if(a.x != b.x) return a.x < b.x;
return a.y < b.y;
}
bool cmpy(const int& a, const int& b)
{
return List[a].y < List[b].y;
}
double dis(int a, int b)
{
return sqrt((List[a].x-List[b].x)*(List[a].x-List[b].x) + (List[a].y-List[b].y)*(List[a].y-List[b].y));
}
double Closest_Pair(int left, int right)
{
double d = 1.0*INF;
if(left==right) return d;
if(left + 1 == right) return dis(left, right);
int mid = (left+right)>>1;
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(List[mid].x-List[i].x) <= d) tmpt[k++] = i;
sort(tmpt,tmpt+k,cmpy);
for(int i = 0; i < k; i++)
{
for(int j = i+1; j < k && List[tmpt[j]].y-List[tmpt[i]].y<d; j++)
{
double d3 = dis(tmpt[i],tmpt[j]);
d=min(d,d3);
}
}
return d;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
for(int i=0; i<n; i++)
List[i].input();
Graham(n);
for(int i=0; i<top; i++)
p[i]=List[Stack[i]];
sort(List,List+n,cmpxy);
printf("%.2lf\n",Closest_Pair(0,n-1)/2);
//printf("%.3lf\n",1.0*sqrt(rotating_calipers(p,top)));
}
return 0;
}