Quoit Design
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25982 Accepted Submission(s): 6909
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
题意:求最近点对的距离的一半
题解:分治最近点对,卡qsort?我擦,sort可以过,竟然qsort会TLE?不懂啊。。。
这里有很详细解释:http://blog.csdn.net/hellobabygogo3/article/details/8042650
分治:
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<math.h>
using namespace std;
struct point{
double x,y;
}p[100008],tp[100008];
int cmpx(struct point a,struct point b)
{
return a.x<b.x;
}
int cmpy(struct point a,struct point b)
{
return a.y<b.y;
}
double dis(struct point p1,struct point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double MIN(double x,double y){ return x<y?x:y; }
double find_shortest(int l,int r)
{
int mid=(l+r)>>1,i,j,all=0;
double ans;
if(l-r==-1) return dis(p[l],p[r]);
if(l-r==-2) return MIN(dis(p[l],p[l+1]),MIN(dis(p[l],p[l+2]),dis(p[l+1],p[l+2])));
ans=MIN(find_shortest(l,mid),find_shortest(mid+1,r));
for(i=l;i<=r;i++)
{
if(fabs(p[mid].x-p[i].x)<ans)
{
tp[all++]=p[i];
}
}
sort(tp,tp+all,cmpy);
for(i=0;i<all;i++)
{
for(j=i+1;j<all;j++)
{
ans=MIN(ans,dis(tp[i],tp[j]));
if(tp[j].y-tp[i].y>=ans) break;
}
}
return ans;
}
int main()
{
int i,n;
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
sort(p,p+n,cmpx);
printf("%.2f\n",find_shortest(0,n-1)/2);
}
}
K-D树:
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 211222;
const int maxD = 2;
const int maxM = 12;
const double INF = 0xffffff;
int now;
struct TPoint {
double x[maxD];
void read() {
for (int i = 0; i < 2; ++i)
scanf("%lf", x + i);
}
} p[maxn];
bool cmp(const TPoint& a, const TPoint& b) {
return a.x[now] < b.x[now];
}
template<typename T> T sqr(T n) {
return n * n;
}
struct KDtree {
int K, n, top;
int split[maxn];
double dis2[maxM];
TPoint stk[maxn];
TPoint kp[maxn];
TPoint mp;
void build(int l, int r) {
if (l >= r)
return;
int i, j, mid = (l + r) >> 1;
double dif[maxD], mx;
for (i = 0; i < K; ++i) {
mx = dif[i] = 0;
for (j = l; j < r; ++j)
mx += kp[j].x[i];
mx /= r - l;
for (j = l; j < r; ++j)
dif[i] += sqr(kp[j].x[i] - mx);
}
now = 0;
for (i = 1; i < K; ++i)
if (dif[now] < dif[i])
now = i;
split[mid] = now;
nth_element(kp + l, kp + mid, kp + r, cmp);
build(l, mid);
build(mid + 1, r);
}
void update(const TPoint& p, int M) {
int i, j;
double tmp = dist(p, mp);
for (i = 0; i < M; ++i)
if (dis2[i] > tmp) {
for (j = M - 1; j > i; --j) {
stk[j] = stk[j - 1];
dis2[j] = dis2[j - 1];
}
stk[i] = p;
dis2[i] = tmp;
break;
}
}
void nearest_search(int l, int r, int M) {
if (l >= r)
return;
int mid = (l + r) >> 1;
update(kp[mid], M);
if (l + 1 == r)
return;
double d = mp.x[split[mid]] - kp[mid].x[split[mid]];
if (d <= 0) {
nearest_search(l, mid, M);
if (sqr(d) < dis2[M - 1])
nearest_search(mid + 1, r, M);
} else {
nearest_search(mid + 1, r, M);
if (sqr(d) < dis2[M - 1])
nearest_search(l, mid, M);
}
}
void find_nearest(TPoint p, int M) {
for (int i = 0; i < M; ++i) {
dis2[i] = INF;
}
mp = p;
nearest_search(0, n, M);
}
double dist(const TPoint& a, const TPoint& b) {
double res = 0;
for (int i = 0; i < K; ++i)
res += sqr<double>(a.x[i] - b.x[i]);
return res;
}
} KD;
int main() {
int i, n;
double res;
while (scanf("%d", &n),n) {
KD.n = n;
KD.K = 2;
for (i = 0; i < n; ++i) {
p[i].read();
KD.kp[i] = p[i];
}
KD.build(0, n);
res=INF;
for (i = 0; i < n; ++i) {
KD.find_nearest(p[i], 2);
res=min(res,KD.dis2[1]);
}
printf("%.2f\n",sqrt(res)/2);
}
return 0;
}