题解:CF744D Hongcow Draws a Circle
非常好计算几何。
首先这种神秘问题一般都需要二分答案,现在二分了半径
考虑圆心的合法位置,圆心距离蓝点必须大于等于
这样还是非常不好做。
但是发现,若半径
所以我们只要判断每个蓝点或红点做半径为
这个问题是简单的,一个蓝色圆会禁止掉圆上的一段区间,一个红色圆会使圆上的一段区间合法。
然后可能需要一些数学知识,若
现在时间复杂度是
考虑经典去掉
时间复杂度
const int N = 1e3 + 5;
const int INF = 1e9;
const double EPS = 1e-5;
const double PI = acos(- 1);
struct Point { double x, y; };
int n, m; Point a[N], b[N];
double sq(double x) {
return x * x;
}
double dist(Point A, Point B) {
return sqrt(sq(A.x - B.x) + sq(A.y - B.y));
}
double mod(double A) {
while(A < 0) A += PI * 2;
while(A >= PI * 2) A -= PI * 2;
return A;
}
double angle(Point A, Point B) {
return mod(atan2(B.y - A.y, B.x - A.x));
}
bool check1(int i, double mid) {
VC<pair<double, int>> e;
FOR(j, 1, m) {
double d = dist(a[i], b[j]);
if(d > mid * 2) continue;
double A = angle(b[j], a[i]);
double B = acos(d / mid / 2);
double L = mod(A - B);
double R = mod(A + B);
e.eb(L, 1); e.eb(R, - 1);
if(L > R) e.eb(0, 1);
}
if(e.empty()) return 1;
sort(begin(e), end(e));
int res = 0;
for(auto [u, x] : e) {
res += x;
if(! res) return 1;
}
return 0;
}
bool check2(int i, double mid) {
VC<pair<double, PII>> e;
FOR(j, 1, m) if(i != j) {
double d = dist(b[i], b[j]);
if(d > mid * 2) continue;
double A = angle(b[j], b[i]);
double B = acos(d / mid / 2);
double L = mod(A - B);
double R = mod(A + B);
e.pb({L, {0, 1}});
e.pb({R, {0, - 1}});
if(L > R) e.pb({0, {0, 1}});
}
FOR(j, 1, n) {
double d = dist(b[i], a[j]);
if(d > mid * 2) continue;
double A = angle(a[j], b[i]);
double B = acos(d / mid / 2);
double L = mod(A - B);
double R = mod(A + B);
e.pb({L, {1, 1}});
e.pb({R, {1, - 1}});
if(L > R) e.pb({0, {1, 1}});
}
int r1 = 0, r2 = 0;
sort(begin(e), end(e));
for(auto [u, h] : e) {
auto [o, x] = h;
(o ? r2 : r1) += x;
if(! r1 && r2) return 1;
}
return 0;
}
bool check(double mid) {
FOR(i, 1, n) if(check1(i, mid)) return 1;
FOR(i, 1, m) if(check2(i, mid)) return 1;
return 0;
}
void solve() {
read(n, m);
FOR(i, 1, n) read(a[i].x, a[i].y);
FOR(i, 1, m) read(b[i].x, b[i].y);
mt19937 rnd(time(0));
shuffle(a + 1, a + n + 1, rnd);
shuffle(b + 1, b + m + 1, rnd);
if(check(INF)) { print(- 1); return; }
double ans = 0;
FOR(i, 1, n) {
if(! check1(i, ans)) continue;
double L = ans, R = INF;
while((R - L) / max(L, 1.) > EPS) {
double mid = (L + R) / 2;
if(check1(i, mid)) L = mid;
else R = mid;
}
ans = L;
}
FOR(i, 1, m) {
if(! check2(i, ans)) continue;
double L = ans, R = INF;
while((R - L) / max(L, 1.) > EPS) {
double mid = (L + R) / 2;
if(check2(i, mid)) L = mid;
else R = mid;
}
ans = L;
}
cout << fixed << setprecision(10);
print(ans);
}