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;
}
相關文章
- 計算幾何
- 計算幾何:模板
- 計算幾何模板
- Something about 計算幾何
- [筆記] 計算幾何筆記
- 計算幾何 —— 二維幾何基礎 —— 距離度量方法
- SGU 124 Broken line(計算幾何)
- 計算幾何——平面最近點對
- POJ - 1556 【計算幾何 + 最短路】
- 【學習筆記】計算幾何筆記
- 邊緣計算、霧計算、雲端計算區別幾何?
- BNUOJ 12887 isumi(計算幾何+最大流)
- SGU 120 SGU 228 Archipelago(計算幾何)Go
- Codeforces Global Round 19 E. Best PairAI
- Educational Codeforces Round 19 E. Array Queries
- CodeForces 145 E. Lucky Queries(線段樹)
- 計算幾何(一):凸包問題(Convex Hull)
- POJ 1113 Wall(思維 計算幾何 數學)
- HNOI2016礦區(計算幾何+對偶圖)
- 計算機視覺—圖片幾何變換(2)計算機視覺
- Codeforces #698 (Div. 2) E. Nezzar and Binary String 題解
- [計算幾何]圓與三角形是否相交
- 百度造車,勝算幾何?
- [幾何]計算不規則多邊形的面積、中心、重心
- 【第一道計算幾何題】 UVA11178 Morley‘s Theorem (二維幾何,旋轉直線求求交點)REM
- 丘成桐演講全文:幾何與計算數學的關係
- 【IDL】幾何圖形數學運算函式函式
- Educational Codeforces Round 98 (Rated for Div. 2) E. Two Editorials 細節題
- 平面幾何
- 神奇的周幾計算器
- Codeforces Good Bye 2017 C. New Year and Curling(運用數學解析幾何的知識判斷)Go
- sql server 計算兩個時間 相差的 幾天幾時幾分幾秒SQLServer
- 《戴森球計劃》火了,“科幻遊戲”前途幾何?遊戲
- 恆訊科技分析:使用邊緣計算和雲端計算各有何優勢?
- 計算檔案Checksum的幾種方法
- 阿里美團戰事再升級:支付寶入局 勝算幾何?阿里
- 邊緣計算為何會如此受歡迎?
- [計算機網路] - 從英雄聯盟,看資料包何去何從?計算機網路