【BZOJ1337】最小圆覆盖

【题目链接】

【双倍经验链接】

【思路要点】

  • 最小圆覆盖模板题。
  • 时间复杂度\(O(N)\)。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const long double eps = 1e-15;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
struct point {long double x, y; } a[MAXN];
struct circle {point pos; long double r; };
point operator + (point a, point b) {return (point) {a.x + b.x, a.y + b.y}; }
point operator - (point a, point b) {return (point) {a.x - b.x, a.y - b.y}; }
point operator * (point a, long double b) {return (point) {a.x * b, a.y * b}; }
long double operator * (point a, point b) {return a.x * b.y - a.y * b.x; }
long double dist(point a, point b) {
	return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
bool inside(point a, circle o) {return dist(a, o.pos) <= o.r + eps; }
point unit(point a) {
	long double tmp = sqrt(a.x * a.x + a.y * a.y);
	return (point) {a.x / tmp, a.y / tmp};
}
point inv(point a) {return (point) {-a.y, a.x}; }
int main() {
	int n; read(n);
	for (int i = 1; i <= n; i++)
		scanf("%Lf%Lf", &a[i].x, &a[i].y);
	random_shuffle(a + 1, a + n + 1);
	circle now = (circle) {a[1], 0};
	for (int i = 1; i <= n; i++) {
		if (inside(a[i], now)) continue;
		now = (circle) {a[i], 0};
		for (int j = 1; j <= i - 1; j++) {
			if (inside(a[j], now)) continue;
			now = (circle) {(a[i] + a[j]) * 0.5, dist(a[i], a[j]) * 0.5};
			for (int k = 1; k <= j - 1; k++) {
				if (inside(a[k], now)) continue;
				point b = (a[i] + a[j]) * 0.5;
				point c = b + inv(a[j] - a[i]);
				point d = (a[j] + a[k]) * 0.5;
				point e = d + inv(a[j] - a[k]);
				long double tmp = (d - b) * (e - b);
				long double tnp = (e - c) * (d - c);
				now.pos = (b * tnp + c * tmp) * (1 / (tmp + tnp));
				now.r = dist(now.pos, a[i]);
			}
		}
	}
	printf("%.3Lf\n", now.r);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值