CodeForces 887 E. Little Brother(計算幾何+二分)
E. Little Brother
Description:
Masha’s little brother draw two points on a sheet of paper. After that, he draws some circles and gave the sheet to his sister.
Masha has just returned from geometry lesson so she instantly noticed some interesting facts about brother’s drawing.
At first, the line going through two points, that brother drew, doesn’t intersect or touch any circle.
Also, no two circles intersect or touch, and there is no pair of circles such that one circle is located inside another.
Moreover, for each circle, Masha drew a square of the minimal area with sides parallel axis such that this circle is located inside the square and noticed that there is no two squares intersect or touch and there is no pair of squares such that one square is located inside other.
Now Masha wants to draw circle of minimal possible radius such that it goes through two points that brother drew and doesn’t intersect any other circle, but other circles can touch Masha’s circle and can be located inside it.
It’s guaranteed, that answer won’t exceed 1012. It should be held for hacks as well.
Input:
First line contains four integers x1, y1, x2, y2 ( - 105 ≤ x1, y1, x2, y2 ≤ 105) — coordinates of points that brother drew. First point has coordinates (x1, y1) and second point has coordinates (x2, y2). These two points are different.
The second line contains single integer n (1 ≤ n ≤ 105) — the number of circles that brother drew.
Next n lines contains descriptions of circles. Each line contains three integers xi, yi, ri ( - 105 ≤ xi, yi ≤ 105, 1 ≤ ri ≤ 105) describing circle with center (xi, yi) and radius ri.
Output
Output smallest real number, that it’s possible to draw a circle with such radius through given points in such a way that it doesn’t intersect other circles.
The output is considered correct if it has a relative or absolute error of at most 10 - 4.
Sample Input:
2 4 7 13
3
3 0 1
12 4 2
-4 14 2
Sample Output:
5.1478150705
Sample Input:
-2 3 10 -10
2
7 0 3
-5 -5 2
Sample Output:
9.1481831923
題目連結
平面上有 個點與 個圓,經過兩點畫圓使其與 個圓不相交(可包含),求最小半徑
經過兩點畫圓則其圓心一定在其兩點線段的中垂線上
考慮二分圓心到兩點中點的距離,找到對於 個圓中每個圓內切、外切的兩個距離
最後選出最小與 個圓不相交的距離求出半徑即可
AC程式碼:
#include <bits/stdc++.h>
using namespace std;
typedef double db;
const db inf = 1e13;
const db eps = 1e-9;
int Sgn(db k) {return fabs(k) < eps ? 0 : (k < 0 ? -1 : 1);}
int Cmp(db k1, db k2) {return Sgn(k1 - k2);}
struct Point {db x, y;};
typedef Point Vector;
Vector operator - (Vector k1, Vector k2) {return (Vector){k1.x - k2.x, k1.y - k2.y};}
Vector operator + (Vector k1, Vector k2) {return (Vector){k1.x + k2.x, k1.y + k2.y};}
db operator * (Vector k1, Vector k2) {return k1.x * k2.x + k1.y * k2.y;}
db operator ^ (Vector k1, Vector k2) {return k1.x * k2.y - k1.y * k2.x;}
Vector operator * (Vector k1, db k2) {return (Vector){k1.x * k2, k1.y * k2};}
Vector operator / (Vector k1, db k2) {return (Vector){k1.x / k2, k1.y / k2};}
db GetLen(Vector k) {return sqrt(k * k);}
db GetDisPointToPoint(Point k1, Point k2) {return sqrt((k2 - k1) * (k2 - k1));}
Vector Rotate(Vector k, db ang) {return (Vector){k.x * cos(ang) - k.y * sin(ang), k.x * sin(ang) + k.y * cos(ang)};}
Vector Rotate90(Vector k) {return (Vector){-k.y, k.x};}
struct Circle {Point o; db r;};
bool IsInside(Circle k1, Circle k2) {
db dis = GetDisPointToPoint(k1.o, k2.o);
if (Cmp(k1.r - k2.r, dis) >= 0 || Cmp(k2.r - k1.r, dis) >= 0) return true;
return false;
}
bool IsOutside(Circle k1, Circle k2) {
db dis = GetDisPointToPoint(k1.o, k2.o);
if (Cmp(dis, k1.r + k2.r) > 0) return true;
return false;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cout << fixed << setprecision(10);
Point s, t;
cin >> s.x >> s.y >> t.x >> t.y;
int n; cin >> n;
vector<Circle> c(n);
for (auto &it : c) cin >> it.o.x >> it.o.y >> it.r;
Point mid_point = (s + t) / 2.0;
Vector dir = Rotate90(t - s) / GetLen(t - s);
db dis_div2 = GetDisPointToPoint(s, mid_point);
bool ans_flag = true;
for (auto &it : c) {
Circle cur = (Circle){mid_point, dis_div2};
if (!IsInside(cur, it) && !IsOutside(cur, it)) {
ans_flag = false;
break;
}
}
if (ans_flag) {
cout << dis_div2 << endl;
return 0;
}
vector<pair<db, db>> record;
for (auto &it : c) {
bool dir_flag;
if (Sgn((it.o - mid_point) * dir) > 0) dir_flag = true;
else dir_flag = false;
db inside_record, outside_record; Circle binary_search_cur;
int binary_search_cnt = 0;
db binary_search_left = -inf, binary_search_right = inf;
while (Cmp(binary_search_left, binary_search_right) && binary_search_cnt++ < 100) {
db binary_search_mid = (binary_search_left + binary_search_right) / 2.0;
binary_search_cur.o = dir * binary_search_mid + mid_point;
binary_search_cur.r = sqrt(binary_search_mid * binary_search_mid + dis_div2 * dis_div2);
if (IsInside(binary_search_cur, it)) {
inside_record = binary_search_mid;
if (dir_flag) binary_search_right = binary_search_mid;
else binary_search_left = binary_search_mid;
}
else {
if (dir_flag) binary_search_left = binary_search_mid;
else binary_search_right = binary_search_mid;
}
}
binary_search_cnt = 0;
binary_search_left = -inf, binary_search_right = inf;
while (Cmp(binary_search_left, binary_search_right) && binary_search_cnt++ < 100) {
db binary_search_mid = (binary_search_left + binary_search_right) / 2.0;
binary_search_cur.o = dir * binary_search_mid + mid_point;
binary_search_cur.r = sqrt(binary_search_mid * binary_search_mid + dis_div2 * dis_div2);
if (IsOutside(binary_search_cur, it)) {
outside_record = binary_search_mid;
if (dir_flag) binary_search_left = binary_search_mid;
else binary_search_right = binary_search_mid;
}
else {
if (dir_flag) binary_search_right = binary_search_mid;
else binary_search_left = binary_search_mid;
}
}
if (Cmp(inside_record, outside_record) > 0) swap(inside_record, outside_record);
record.push_back(make_pair(inside_record, outside_record));
}
sort(record.begin(), record.end(), [&](pair<db, db> k1, pair<db, db> k2) {return Cmp(k1.first, k2.first) < 0;});
db ans = Cmp(-record[0].first, 0.0) > 0 ? -record[0].first : 0.0;
db fi = record[0].first, se = record[0].second;
for (int i = 1; i < record.size(); ++i) {
if (Cmp(se, record[i].first) < 0) {
if (Cmp(ans, fabs(se)) > 0) ans = fabs(se);
if (Cmp(ans, fabs(record[i].first)) > 0) ans = fabs(record[i].first);
fi = record[i].first;
}
if (Cmp(record[i].second, se) > 0) se = record[i].second;
}
if (Cmp(ans, fabs(se)) > 0) ans = fabs(se);
cout << sqrt(ans * ans + dis_div2 * dis_div2) << endl;
return 0;
}
相關文章
- 計算幾何
- 計算幾何:模板
- 計算幾何模板
- [筆記] 計算幾何筆記
- codeforces #340B(簡單計算幾何 自己想複雜了)
- 【計算幾何】向量表示
- 【總結】計算幾何模板
- 二維計算幾何模板
- 【計算幾何】線段相交
- 三維計算幾何模板
- Something about 計算幾何
- 計算幾何 —— 二維幾何基礎 —— 距離度量方法
- 邊緣計算、霧計算、雲端計算區別幾何?
- 【學習筆記】計算幾何筆記
- 計算幾何_向量的實現
- 【計算幾何】多邊形交集
- 計算幾何——平面最近點對
- POJ - 1556 【計算幾何 + 最短路】
- An Easy Problem?! POJ 2826 計算幾何
- 計算幾何常用的函式/方法函式
- Codeforces Gym 100286A. Aerodynamics 計算幾何 求二維凸包面積
- BNUOJ 12887 isumi(計算幾何+最大流)
- SGU 124 Broken line(計算幾何)
- 【計算幾何】Triangles HUST 1607
- 【計算幾何】多邊形點集排序排序
- C++計算幾何演算法大全C++演算法
- POJ 2991 Crane(線段樹+計算幾何)
- 【計算幾何】點在多邊形內部
- POJ 1556 The Doors(Dijkstra+計算幾何)
- Codeforces Global Round 19 E. Best PairAI
- Educational Codeforces Round 19 E. Array Queries
- ZOJ 3806 Incircle and Circumcircle(幾何+二分)
- 二維幾何常用運算
- SGU 120 SGU 228 Archipelago(計算幾何)Go
- 計算機視覺—圖片幾何變換(2)計算機視覺
- 【計算幾何】求線段相交交點座標
- BZOJ 1027 合金 計算幾何,Floyd判環
- CG_Hadoop:基於MapReduce的計算幾何Hadoop