原题http://acm.hdu.edu.cn/showproblem.php?pid=3124
Moonmist
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1682 Accepted Submission(s): 534
Problem Description
An Unidentified Flying Object (Commonly abbreviated as UFO) is the popular term for any aerial phenomenon whose cause cannot be easily or immediately identified. We always believe UFO is the vehicle of aliens. But there is an interrogation about why UFO always likes a circular saucer? There must be a reason. Actually our scientists are developing a new traffic system “Moonmist”. It is distinguished from the traditional traffic. We use circular saucers in this new traffic system and the saucers moves extremely fast. When our scientists did their test, they found that traffic accident was too hard to be avoided because of the high speed of the advanced saucer. They need us to develop a system that can tell them the nearest saucer. The distance between two saucers is defined as the shortest distance between any two points in different saucers.
Input
The first line consists of an integer T, indicating the number of test cases.
The first line of each case consists of an integer N, indicating the number of saucers. Each saucer is represented on a single line, consisting of three integers X, Y, R, indicating the coordinate and the radius. You can assume that the distance between any two saucers will never be zero.
The first line of each case consists of an integer N, indicating the number of saucers. Each saucer is represented on a single line, consisting of three integers X, Y, R, indicating the coordinate and the radius. You can assume that the distance between any two saucers will never be zero.
Output
For each test case, please output a floating number with six fractional numbers, indicating the shortest distance.
Constraints
0 < T <= 10
2 <= N <= 50000
0 <= X, Y, R <= 100000
Constraints
0 < T <= 10
2 <= N <= 50000
0 <= X, Y, R <= 100000
Sample Input
1 2 0 0 1 10 10 1
Sample Output
12.142136
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<set>
#include<algorithm>
#include <math.h>
using namespace std;
set <int>tree;
set <int>::iterator iter;
struct Point
{
double x;
int id, flag;
} p1[100001], p2[100001];
int tot1, tot2;
struct Q
{
double x,y, r;
} q[50001];
int cmp(const void*p1, const void*p2)
{
struct Point*a1=(struct Point*)p1;
struct Point*a2=(struct Point*)p2;
if (a1->x<a2->x) return -1;
else if (a1->x==a2->x) return a2->flag-a1->flag;
else return 1;
}
int cmp1(const void*p1, const void*p2)
{
struct Q*a1=(struct Q*)p1;
struct Q*a2=(struct Q*)p2;
if (a1->y<a2->y)return -1;
else if (a1->y==a2->y)return 0;
else return 1;
}
double dis(double x1, double y1, double x2, double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
bool judge(int i, int j, double d)
{
if (dis(q[i].x, q[i].y, q[j].x, q[j].y)<=q[i].r+q[j].r+2.0*d)
return true;
return false;
}
bool insert(int v,double d)
{
iter = tree.insert(v).first;
if (iter != tree.begin())
{
if (judge(v, *--iter,d))
{
return true;
}
++iter;
}
if (++iter != tree.end())
{
if (judge(v, *iter,d))
{
return true;
}
}
return false;
}
bool remove(int v,double d)
{
iter = tree.find(v);
if (iter != tree.begin() && iter != --tree.end())
{
int a = *--iter;
++iter;
int b = *++iter;
if (judge(a, b,d))
{
return true;
}
}
tree.erase(v);
return false;
}
bool check(double d)
{
int i=1, j=1;
while (i<=tot1&&j<=tot2)
{
if (p1[i].x-d<=p2[j].x+d)
{
if (insert(p1[i++].id, d))
return true;
}
else
{
if (remove(p2[j++].id, d))
return true;
}
}
while (i<=tot1)
{
if (insert(p1[i++].id, d))
return true;
}
while (j<=tot2)
{
if (remove(p2[j++].id, d))
return true;
}
return false;
}
int main ()
{
int cases, n, i;
scanf("%d",&cases);
while (cases--)
{
scanf("%d",&n);
tot1=tot2=0;
for (i=1; i<=n; i++)
scanf("%lf %lf %lf",&q[i].x,&q[i].y, &q[i].r);
qsort(q+1,n,sizeof(struct Q),cmp1);
for (i=1; i<=n; i++)
{
tot1++;
p1[tot1].x=q[i].x-q[i].r;
p1[tot1].id=i;
p1[tot1].flag=1;
tot2++;
p2[tot2].x=q[i].x+q[i].r;
p2[tot2].id=i;
p2[tot2].flag=-1;
}
qsort(p1+1,tot1,sizeof(struct Point),cmp);
qsort(p2+1,tot2,sizeof(struct Point),cmp);
double head=0.0, tail=dis(q[1].x,q[1].y,q[2].x,q[2].y)+1.0, mid;
while (tail-head>1e-8)
{
tree.clear();
mid=(head+tail)/2.0;
if (check(mid))
{
tail=mid;
}
else head=mid;
}
printf ("%.6lf\n",2.0*head);
}
return 0;
}