Moonmist
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1837 Accepted Submission(s): 618
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.
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
Sample Input
Sample Output
题意是从n个不相交的圆里找到最近的一对圆的距离.
首先二分结果,然后更新每个圆的左右端点,扫描线在从左往右的过程中,如果扫到圆的左端点,在加入之前判断是不是和上下相邻的圆相交,如果扫到圆的右端点,在删除前也判断下是不是和上下相邻的圆相交.每次发现相交情况及时的return.
复杂度O(n*lgn*lgm),m是自己设的常数,只要比100000*sqrt (2)都可以.
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <iostream>
using namespace std;
#define maxn 111111
#define pb push_back
#define mp make_pair
#define eps 1e-8
double x[maxn], y[maxn], r[maxn];
double rr[maxn];
int n;
vector <pair<double, int> > a;
set <pair<double, int> > gg;
bool xiangjiao (int i, int j) { //判断圆i和圆j是不是相交
double xx = x[i]-x[j], yy = y[i]-y[j];
return sqrt (xx*xx+yy*yy) <= rr[i]+rr[j];
}
void stop () {
while (1) ;
}
bool judge () {
sort (a.begin (), a.end ());
for (int i = 0; i < a.size (); i++) {
int id = a[i].second;
if (id <= n) { //圆的左边界
set <pair <double, int> >::iterator it = gg.lower_bound (mp (y[id], id));
if (it != gg.end ()) {
if (xiangjiao (id, it->second))
return 1;
}
if (it != gg.begin ()) {
if (xiangjiao (id, (--it)->second))
return 1;
}
gg.insert (mp (y[id], id));
}
else { //圆的右边界
id -= n;
set <pair <double, int> >::iterator it = gg.upper_bound (mp (y[id], id));
if (it != gg.end ()) {
if (xiangjiao (id, it->second))
return 1;
}
if (it != gg.begin ()) {
--it;
if (it != gg.begin () && xiangjiao (id, (--it)->second))
return 1;
}
gg.erase (mp (y[id], id));
}
}
return 0;
}
int main () {
//freopen ("in", "r", stdin);
int t;
scanf ("%d", &t);
while (t--) {
scanf ("%d", &n);
for (int i = 1; i <= n; i++) {
scanf ("%lf%lf%lf", &x[i], &y[i], &r[i]);
}
double L = 0.0, R = 1000000;
while (R-L > eps) { //二分距离
double mid = (L+R)/2.0;
a.clear (); gg.clear ();
for (int i = 1; i <= n; i++) {
rr[i] = r[i] + mid/2.0;
a.pb (mp (x[i]-r[i]-mid/2.0, i));
a.pb (mp (x[i]+r[i]+mid/2.0, i+n));
}
if (judge ()) //存在交点
R = mid;
else { //不存在交点
L = mid;
}
}
printf ("%.6f\n", (L+R)/2.0);
}
return 0;
}