该死的精度问题让我调了一个小时还没有调出来(事实说明我还是太菜了。)

大概意思就是说平面上给了你一些点然后你要构造一个与 x 轴相切的圆把这些圆都包裹进去。

考虑二分圆的半径,这样就知道了圆心的 y 坐标,根据点在圆内的充要条件——点到圆心的距离小于等于半径计算出圆心的 x 坐标的范围。如果存在两个范围不相交,说明无法构造出一个符合条件的圆心。

需要注意浮点数运算的精度问题,比如计算 x 坐标的范围时这么写:

1
dis = r * r - (r - y) * (r - y);

就容易被卡,而:

1
dis = 2 * r * y - y * y;

就不容易。

代码:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// ==============================
// author: memset0
// website: https://memset0.cn
// ==============================
#include <bits/stdc++.h>
#define ll long long
#define rep(i,l,r) for (int i = l; i <= r; i++)
#define getc(x) getchar(x)
#define putc(x) putchar(x)

template <typename T> inline void read(T &x) {
x = 0; register char ch; register bool fl = 0;
while (ch = getc(), ch < 48 || 57 < ch) fl ^= ch == '-'; x = (ch & 15);
while (ch = getc(), 47 < ch && ch < 58) x = (x << 1) + (x << 3) + (ch & 15);
if (fl) x = -x;
}
template <typename T> inline void readc(T &x) {
while (x = getc(), !islower(x) && !isupper(x));
}
template <typename T> inline void print(T x, char c = ' ') {
static int buf[40];
if (x == 0) { putc('0'); putc(c); return; }
if (x < 0) putc('-'), x = -x;
for (buf[0] = 0; x; x /= 10) buf[++buf[0]] = x % 10 + 48;
while (buf[0]) putc((char) buf[buf[0]--]);
putc(c);
}

const int maxn = 100010;
const long double eps = 1e-7;

int n, cnt = 1000;
int x[maxn], y[maxn];

bool flag1, flag2;

long double l, r, mid, ans, tmp;
long double dis, min_rx, max_lx;
long double lx[maxn], rx[maxn];

bool check(long double r) {
for (int i = 1; i <= n; i++) {
tmp = (2 * r - y[i]) * y[i];
if (tmp < 0)
return false;
dis = sqrt(tmp);
lx[i] = x[i] - dis;
rx[i] = x[i] + dis;
}
min_rx = rx[1];
for (int i = 2; i <= n; i++)
min_rx = std::min(min_rx, rx[i]);
max_lx = lx[1];
for (int i = 2; i <= n; i++)
max_lx = std::max(max_lx, lx[i]);
if (max_lx > min_rx)
return false;
return true;
}

int main() {

read(n);
for (int i = 1; i <= n; i++) {
read(x[i]);
read(y[i]);
}
for (int i = 1; i <= n; i++)
if (y[i] < 0) flag1 = 1;
else if (y[i] > 0) flag2 = 1;
if (flag1 && flag2) {
print(-1, '\n');
return 0;
}
if (flag1) {
for (int i = 1; i <= n; i++)
y[i] = -y[i];
}

l = 0, r = 1000000000000001;
while (r - l >= eps && --cnt) {
mid = (l + r) * 0.5;
if (check(mid)) {
ans = mid;
r = mid;
} else {
l = mid;
}
}
std::cout << std::fixed;
std::cout << std::setprecision(8) << ans;
std::cout << std::endl;

return 0;
}