けんちょんの競プロ精進記録

競プロの精進記録や小ネタを書いていきます

AOJ 2972 All your base are belong to us (JAG 夏合宿 2019 day1-F)

今日の ABC 151 F で、「三分探索」とか「山登り法」とか聞いたので!!!

問題へのリンク

問題概要

二次元平面上に  N 個の点が与えられる。
今、好きな位置に点を打って、その点から  N 個の点との距離のうち、大きい順に  K 個の総和をとる。

上手に点を打ったときの、この総和の最小値を求めよ。

制約

  •  1 \le K \le N \le 200

解法

公式解説はここにある

https://jag-icpc.org/?plugin=attach&refer=2019%2FPractice%2F%E5%A4%8F%E5%90%88%E5%AE%BF%2F%E8%AC%9B%E8%A9%95&openfile=F.pdf

さて、 N 個から  K 個選んだものの最適化系の問題は、できることがあまり多くないイメージ。貪欲か、Alien DP みたいな方法か。

今回は、なんと

  •  f(x, y) = 点  (x, y) から  N 点への距離のうち、大きい順に  K 個総和した値

とすると、これは凸関数なのだ。証明は公式解説にある。それを生かして、三分探索したり、山登り法をしたりすることで解くことができる。

三分探索

 x 座標を固定したときの最適解を求める関数  f(x) を三分探索で最適化する。 f(x) の計算自体も  y の位置の最適化を三分探索する。

#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, K;
vector<Point> v;

DD f(DD x, DD y) {
    vector<DD> dis(N);
    for (int i = 0; i < N; ++i) dis[i] = abs(v[i] - Point(x, y));
    sort(dis.begin(), dis.end(), greater<DD>());
    DD res = 0;
    for (int i = 0; i < K; ++i) res += dis[i];
    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 >> K;
    v.resize(N);
    for (int i = 0; i < N; ++i) cin >> v[i].x >> v[i].y;
    cout << fixed << setprecision(10) << solve() << endl;
}