题解 - CF1882B Fear of the Dark

解法

利用一个 distance 函数用于计算两点之间的欧几里德距离。

在主函数中,读取测试用例的数量,处理每个测试用例。
对于每个测试用例,计算 $PA$、$PB$、$OA$ 和 $OB$ 的距离。
使用条件语句来确定 $w$ 的最小值,最后将结果打印为精确到小数点后 $10$ 位的浮点数。

使用直观的方法来比较距离,并找到问题要求的 $w$ 的最小值。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <bits/stdc++.h>
using namespace std;

long double distance(long double x1, long double y1, long double x2, long double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

int main() {
int t;
cin >> t;

while (t--) {
long double px, py, ax, ay, bx, by;
cin >> px >> py >> ax >> ay >> bx >> by;

long double PA = distance(px, py, ax, ay);
long double PB = distance(px, py, bx, by);
long double OA = distance(0.0, 0.0, ax, ay);
long double OB = distance(0.0, 0.0, bx, by);

if (PA < PB && OA < OB)
cout << fixed << setprecision(10) << max(OA, PA) << endl;
else if (PA > PB && OA > OB)
cout << fixed << setprecision(10) << max(OB, PB) << endl;
else if (PA < PB && OA > OB)
cout << fixed << setprecision(10) << min(min(max(OA, PA), max(OB, PB)), max(max(PA, OB), distance(ax, ay, bx, by) / 2.0)) << endl;
else
cout << fixed << setprecision(10) << min(min(max(OA, PA), max(OB, PB)), max(max(PB, OA), distance(ax, ay, bx, by) / 2.0)) << endl;
}

return 0;
}