幾何だ!!!!!
そして、こういうので「ギリギリを考える」というのは典型な感じ。
なお、僕は最小包含円のことを知らず、アドホックに解いたけど、ライブラリ貼るだけだったらしい... (その方が計算量も少ない)
他にも、三分探索でも解ける!!!
問題概要
二次元平面上に 個の点 が与えられる。これらをすべて覆う円のうち、半径の最小値を求めよ。
制約
僕の考えたこと (解法 (1))
幾何では特に多い論法だけど、一般に最適化問題において、以下の論法はものすごくよく見られる。
もしある解が最適解であったとするならば、その解を悪化させることなく、「〜〜〜」という条件を満たすような解に変形することができる
ゆえに、はじめから「〜〜〜」という条件を満たすもののみを探索したとしても、その中に最適解は含まれている
この論法は幾何だと特に多くて、今回の問題でもバッチリはまる。少し考えてみると、一般に 点を囲うような円があったとき、それをギリギリまで半径を小さくしようとすると、三点が引っかかるような感じになるはず。もしくは、二点の直径になるはず (下図のような場合もある)。
というわけで、 点を囲うようなどんな円も、その半径を悪化させることなく、
- 点のうちの 3 点を通る円
- 点のうちの 2 点を直径とする円
のいずれかに変形できることがわかった。つまり、初めからこれらを探索候補に絞ってもよい!!!
なお、ここでは「円」という言い方をしたが、「円の中心」として考えられる場所を絞った、という風に考えることにする。「円の中心」として考えられる候補が 通りに絞られたということになる。
円の中心 を 1 つ決めたときの半径の最小値は、単純に点 から 個の点への距離の最大値を求めれば OK。
3 点を通る円の中心 (外心)
「2 点を直径とする円の中心」は簡単。その 2 点の中点をとるだけ。問題は、「3 点を通る円の中心 (つまり外心)」の求め方。
ここでは、以下の幾何ライブラリを用いることにした。これらはいずれも幾何の問題を少し解いていると自然に整備されているものだと思う。
- 3 点 が同一直線上にあるかどうかを判定する (ccw)
- 2 点 の垂直二等分線を求める
- 2 直線の交点を求める
これらのライブラリがあれば、以下のようにして 3 点 の外心を求めることができる。
- が同一直線上だったら、外心は存在しないので考えない
- 2 点 の垂直二等分線を とし、 の垂直二等分線を とする
- 2 直線 の交点を求める
なお、普通に外心を求める公式を適用してもいいかもしれない。
#include <iostream> #include <vector> #include <cmath> #include <iomanip> using namespace std; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } /* 幾何ライブラリ */ using DD = double; const DD INF = 1LL<<60; // to be set appropriately const DD EPS = 1e-10; // to be set appropriately const DD PI = acosl(-1.0); DD torad(int deg) {return (DD)(deg) * PI / 180;} DD todeg(DD ang) {return ang * 180 / PI;} /* Point */ struct Point { DD x, y; Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {} friend ostream& operator << (ostream &s, const Point &p) {return s << '(' << p.x << ", " << p.y << ')';} }; inline Point operator + (const Point &p, const Point &q) {return Point(p.x + q.x, p.y + q.y);} inline Point operator - (const Point &p, const Point &q) {return Point(p.x - q.x, p.y - q.y);} inline Point operator * (const Point &p, DD a) {return Point(p.x * a, p.y * a);} inline Point operator * (DD a, const Point &p) {return Point(a * p.x, a * p.y);} inline Point operator * (const Point &p, const Point &q) {return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x);} inline Point operator / (const Point &p, DD a) {return Point(p.x / a, p.y / a);} inline Point conj(const Point &p) {return Point(p.x, -p.y);} inline Point rot(const Point &p, DD ang) {return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y);} inline Point rot90(const Point &p) {return Point(-p.y, p.x);} inline DD cross(const Point &p, const Point &q) {return p.x * q.y - p.y * q.x;} inline DD dot(const Point &p, const Point &q) {return p.x * q.x + p.y * q.y;} inline DD norm(const Point &p) {return dot(p, p);} inline DD abs(const Point &p) {return sqrt(dot(p, p));} inline DD amp(const Point &p) {DD res = atan2(p.y, p.x); if (res < 0) res += PI*2; return res;} inline bool eq(const Point &p, const Point &q) {return abs(p - q) < EPS;} inline bool operator < (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y);} inline bool operator > (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y);} inline Point operator / (const Point &p, const Point &q) {return p * conj(q) / norm(q);} /* Line */ struct Line : vector<Point> { Line(Point a = Point(0.0, 0.0), Point b = Point(0.0, 0.0)) { this->push_back(a); this->push_back(b); } friend ostream& operator << (ostream &s, const Line &l) {return s << '{' << l[0] << ", " << l[1] << '}';} }; // 1:a-bから見てcは左側(反時計回り)、-1:a-bから見てcは右側(時計回り)、0:一直線上 int simple_ccw(const Point &a, const Point &b, const Point &c) { if (cross(b-a, c-a) > EPS) return 1; if (cross(b-a, c-a) < -EPS) return -1; return 0; } // 円や直線の交点 vector<Point> crosspoint(const Line &l, const Line &m) { vector<Point> res; DD d = cross(m[1] - m[0], l[1] - l[0]); if (abs(d) < EPS) return vector<Point>(); res.push_back(l[0] + (l[1] - l[0]) * cross(m[1] - m[0], m[1] - l[0]) / d); return res; } // 外心 Point gaisin(Point a, Point b, Point c) { Line ab((a+b)/2, (a+b)/2 + rot90(a-b)); Line bc((b+c)/2, (b+c)/2 + rot90(b-c)); return crosspoint(ab, bc)[0]; } int main() { int N; cin >> N; vector<Point> v(N); for (int i = 0; i < N; ++i) cin >> v[i].x >> v[i].y; // 候補 vector<Point> alt; for (int i = 0; i < N; ++i) { for (int j = i+1; j < N; ++j) { alt.push_back( (v[i] + v[j]) / 2 ); for (int k = j+1; k < N; ++k) { if (simple_ccw(v[i], v[j], v[k]) == 0) continue; auto r = gaisin(v[i], v[j], v[k]); alt.push_back(r); } } } DD res = INF; for (auto r : alt) { DD tmp = 0; for (auto p : v) chmax(tmp, abs(p - r)); chmin(res, tmp); } cout << fixed << setprecision(10) << res << endl; }
最小包含円問題 (解法 (2))
tubo さんのページに解説がある。平均的にならし で解けるらしい!!!すごい!!!
三分探索 (解法 (3))
点 を中心に決めたときの、 点との距離の最大値を とおくと、これは凸関数になっている。よって三分探索することができる。
座標を固定したときの最適解を求める関数 を三分探索で最適化する。 の計算自体も、 の位置の最適化を三分探索する。
#include <iostream> #include <iomanip> #include <vector> #include <cmath> #include <algorithm> using namespace std; using DD = double; const DD INF = 1LL<<60; // to be set appropriately const DD EPS = 1e-10; // to be set appropriately const DD PI = acosl(-1.0); DD torad(int deg) {return (DD)(deg) * PI / 180;} DD todeg(DD ang) {return ang * 180 / PI;} /* Point */ struct Point { DD x, y; Point(DD x = 0.0, DD y = 0.0) : x(x), y(y) {} friend ostream& operator << (ostream &s, const Point &p) {return s << '(' << p.x << ", " << p.y << ')';} }; inline Point operator + (const Point &p, const Point &q) {return Point(p.x + q.x, p.y + q.y);} inline Point operator - (const Point &p, const Point &q) {return Point(p.x - q.x, p.y - q.y);} inline Point operator * (const Point &p, DD a) {return Point(p.x * a, p.y * a);} inline Point operator * (DD a, const Point &p) {return Point(a * p.x, a * p.y);} inline Point operator * (const Point &p, const Point &q) {return Point(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x);} inline Point operator / (const Point &p, DD a) {return Point(p.x / a, p.y / a);} inline Point conj(const Point &p) {return Point(p.x, -p.y);} inline Point rot(const Point &p, DD ang) {return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y);} inline Point rot90(const Point &p) {return Point(-p.y, p.x);} inline DD cross(const Point &p, const Point &q) {return p.x * q.y - p.y * q.x;} inline DD dot(const Point &p, const Point &q) {return p.x * q.x + p.y * q.y;} inline DD norm(const Point &p) {return dot(p, p);} inline DD abs(const Point &p) {return sqrt(dot(p, p));} inline DD amp(const Point &p) {DD res = atan2(p.y, p.x); if (res < 0) res += PI*2; return res;} inline bool eq(const Point &p, const Point &q) {return abs(p - q) < EPS;} inline bool operator < (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x < q.x : p.y < q.y);} inline bool operator > (const Point &p, const Point &q) {return (abs(p.x - q.x) > EPS ? p.x > q.x : p.y > q.y);} inline Point operator / (const Point &p, const Point &q) {return p * conj(q) / norm(q);} const int ITER = 100; int N; vector<Point> v; DD f(DD x, DD y) { DD res = 0; for (int i = 0; i < N; ++i) res = max(res, abs(v[i] - Point(x, y))); return res; } DD g(DD x) { DD left = -1000, right = 1000; for (int _ = 0; _ < ITER; ++_) { DD m1 = (left * 2 + right) / 3; DD m2 = (left + right * 2) / 3; if (f(x, m1) > f(x, m2)) left = m1; else right = m2; } return f(x, left); } DD solve() { DD left = -1000, right = 1000; for (int _ = 0; _ < ITER; ++_) { DD m1 = (left * 2 + right) / 3; DD m2 = (left + right * 2) / 3; if (g(m1) > g(m2)) left = m1; else right = m2; } return g(left); } int main() { cin >> N; v.resize(N); for (int i = 0; i < N; ++i) cin >> v[i].x >> v[i].y; cout << fixed << setprecision(10) << solve() << endl; }