Skip to content

Commit 5070165

Browse files
committed
boj 11662 민호와 강호
1 parent cbbe189 commit 5070165

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

ternarysearch/11662.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <cmath>
4+
using namespace std;
5+
typedef pair<double, double> pdd;
6+
7+
typedef struct Point {
8+
double x1, y1, x2, y2;
9+
}Point;
10+
11+
Point a, b;
12+
13+
double getDis(pdd p, pdd q) {
14+
return sqrt((p.first - q.first) * (p.first - q.first) + (p.second - q.second) * (p.second - q.second));
15+
}
16+
17+
pdd getNextPos(Point p, double per) {
18+
return { p.x1 + (p.x2 - p.x1) * per, p.y1 + (p.y2 - p.y1) * per };
19+
}
20+
21+
void func() {
22+
double l = 0;
23+
double r = 1;
24+
double ans = 1e9;
25+
while (r - l >= 1e-8) {
26+
double p = (l * 2 + r) / 3.0;
27+
double q = (l + r * 2) / 3.0;
28+
29+
pdd ap = getNextPos(a, p);
30+
pdd aq = getNextPos(a, q);
31+
pdd bp = getNextPos(b, p);
32+
pdd bq = getNextPos(b, q);
33+
34+
double pDis = getDis(ap, bp);
35+
double qDis = getDis(aq, bq);
36+
37+
ans = min(ans, min(pDis, qDis));
38+
39+
if (pDis < qDis) {
40+
r = q;
41+
}
42+
else {
43+
l = p;
44+
}
45+
}
46+
47+
cout << fixed;
48+
cout.precision(7);
49+
cout << ans << '\n';
50+
}
51+
52+
void input() {
53+
cin >> a.x1 >> a.y1 >> a.x2 >> a.y2 >> b.x1 >> b.y1 >> b.x2 >> b.y2;
54+
}
55+
56+
int main() {
57+
cin.tie(NULL); cout.tie(NULL);
58+
ios::sync_with_stdio(false);
59+
60+
input();
61+
func();
62+
63+
return 0;
64+
}

0 commit comments

Comments
 (0)