跳到正文

题解

题解:CF744D Hongcow Draws a Circle

ケロシ约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 5 分钟
题目
CF744D

版本

  1. v2当前
  2. v1

非常好计算几何。

首先这种神秘问题一般都需要二分答案,现在二分了半径 rr。

现在时间复杂度是 O(n2log⁡nlog⁡V)O(n^2 \log n \log V),无法通过。

时间复杂度 O(n2log⁡n)O(n^2 \log n),瓶颈在排序。

C++
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);
}
原文