跳到正文

题解

题解:P3517 [POI 2011] WYK-Plot

#SUSPENDED#约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 8 分钟
获赞
1
题目
P3517

版本

  1. v1当前

二分时采用左端点倍增的方法优化时间复杂度

TalkTalk isis cheapcheap ,, showshow aa codecode ..

C++
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef double db;
typedef long double lb;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
const ll inf = 1e18;
const double eps = 1e-9;
int n, m;
double R, minn;
pair<double, double> ans, a[N], b[N];
vector<pair<double, double>> Ans;
vector<int> Pos;
inline ll read() {
    ll x = 0, m = 1;
    char ch = getchar();

    while (!isdigit(ch)) {
        if (ch == '-')
            m = -1;

        ch = getchar();
    }

    while (isdigit(ch)) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }

    return x * m;
}
inline void write(ll x) {
    if (x < 0) {
        putchar('-');
        write(-x);
        return;
    }

    if (x >= 10)
        write(x / 10);

    putchar(x % 10 + '0');
}

inline double dis(pair<double, double> x, pair<double, double> y) {
    return (double) sqrt((x.first - y.first) * (x.first - y.first) + (x.second - y.second) *
                         (x.second - y.second));
}
inline void get(pair<double, double> x, pair<double, double> y, pair<double, double> z) {
    double a1 = y.first - x.first, a2 = z.first - x.first, b1 = y.second - x.second, b2 = z.second - x.second;
    double c1 = (y.first * y.first - x.first * x.first + y.second * y.second - x.second * x.second);
    double c2 = (z.first * z.first - x.first * x.first + z.second * z.second - x.second * x.second);
    ans.first = (b2 * c1 - b1 * c2) / (b2 * a1 * 2 - b1 * a2 * 2);
    ans.second = (a2 * c1 - a1 * c2) / (a2 * b1 * 2 - a1 * b2 * 2);
    R = dis(x, ans);
}
inline pair<double, double> work(int n) {
    R = 0, ans = a[1];

    for (int i = 2; i <= n; ++ i) {
        if (dis(ans, a[i]) > R + eps) {
            ans = a[i], R = 0;

            for (int j = 1; j < i; ++ j) {
                if (dis(ans, a[j]) > R + eps) {
                    ans.first = (a[j].first + a[i].first) / 2, ans.second = (a[j].second + a[i].second) / 2;
                    R = dis(ans, a[j]);

                    for (int k = 1; k < j; ++ k)
                        if (dis(ans, a[k]) > R + eps)
                            get(a[i], a[j], a[k]);
                }
            }
        }
    }

    return ans;
}
inline pair<double, double> getans(int l, int r) {
    for (int i = l; i <= r; ++ i)
        a[i - l + 1] = b[i];

    random_shuffle(a + 1, a + r - l + 2);
    return work(r - l + 1);
}
inline bool check(double k) {
    int pos = 1;
    vector<pair<double, double>> vec;
    vector<int> vec2;
    vec2.pb(1);

    while (pos <= n) {
        int Len = 1;

        while (Len <= n - pos + 1) {
            Len *= 2;
            getans(pos, min(n, pos + Len - 1));

            if (R - k > eps)
                break;
        }

        int l = pos + Len / 2 - 1, r = min(n, pos + Len - 1), now = l;

        while (l <= r) {
            int mid = (l + r) >> 1;
            getans(pos, mid);

            if (R <= k - eps)
                l = mid + 1, now = mid;
            else
                r = mid - 1;
        }

        getans(pos, now);
        vec.pb(ans), vec2.pb(now + 1), pos = now + 1;
    }

    if (vec.size() > m)
        return 0;

    Ans = vec, Pos = vec2;
    return 1;
}
inline void chk() {
    for (int i = 0; i < Pos.size() - 1; ++ i)
        write(Pos[i]), putchar(' '), write(Pos[i + 1] - 1), putchar('\n');
}
inline void solve() {
    n = read(), m = read();

    for (int i = 1; i <= n; ++ i)
        cin >> b[i].first >> b[i].second;

    double l = 0, r = 2000000.000000;

    while (r - l > eps) {
        double mid = (l + r) / 2.000000;

        if (check(mid))
            r = mid, minn = mid;
        else
            l = mid;
    }

    printf("%.10lf\n", minn), write(Ans.size()), putchar('\n');

    for (pair<double, double> u : Ans)
        printf("%.10lf %.10lf\n", u.first, u.second);
}
signed main() {
    int T = 1;

    //  T = read();
    while (T --)
        solve();

    return 0;
}
原文