跳到正文

闲话

帮我解题

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

文章信息

收录
2026年5月21日
更新
2026年5月22日
篇幅
约 8 分钟

版本

  1. v1当前
C++
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

const int MAXN = 100010;
const double EPS = 1e-8;
const double INF = 1e18;

struct Point {
    double x, y;
    Point() {}
    Point(double x, double y) : x(x), y(y) {}
};

// 两点距离平方
inline double dist2(const Point& a, const Point& b) {
    return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
}

// 两点距离
inline double dist(const Point& a, const Point& b) {
    return sqrt(dist2(a, b));
}

// 三点求外接圆
Point get_circle(const Point& A, const Point& B, const Point& C) {
    double a1 = 2*(B.x - A.x), b1 = 2*(B.y - A.y);
    double c1 = B.x*B.x + B.y*B.y - A.x*A.x - A.y*A.y;
    double a2 = 2*(C.x - A.x), b2 = 2*(C.y - A.y);
    double c2 = C.x*C.x + C.y*C.y - A.x*A.x - A.y*A.y;
    double d = a1*b2 - a2*b1;
    double x = (b2*c1 - b1*c2)/d;
    double y = (a1*c2 - a2*c1)/d;
    return Point(x, y);
}

// 随机增量法求最小圆覆盖
Point min_circle(const vector<Point>& p, int l, int r, double& radius) {
    vector<Point> tmp;
    for (int i = l; i <= r; ++i) tmp.push_back(p[i]);
    random_shuffle(tmp.begin(), tmp.end());
    Point O = tmp[0];
    radius = 0;
    for (int i = 0; i < tmp.size(); ++i) {
        if (dist2(tmp[i], O) > radius*radius + EPS) {
            O = tmp[i];
            radius = 0;
            for (int j = 0; j < i; ++j) {
                if (dist2(tmp[j], O) > radius*radius + EPS) {
                    O = Point((tmp[i].x + tmp[j].x)/2, (tmp[i].y + tmp[j].y)/2);
                    radius = dist(tmp[i], tmp[j])/2;
                    for (int k = 0; k < j; ++k) {
                        if (dist2(tmp[k], O) > radius*radius + EPS) {
                            O = get_circle(tmp[i], tmp[j], tmp[k]);
                            radius = dist(tmp[k], O);
                        }
                    }
                }
            }
        }
    }
    return O;
}

int n, m;
vector<Point> p;
vector<int> seg_end; // 记录每一段的结束位置

// 检查d是否可行,并记录分段
bool check(double d) {
    seg_end.clear();
    int pos = 0;
    int cnt = 0;
    while (pos < n) {
        cnt++;
        if (cnt > m) return false;
        // 倍增找最大长度
        int len = 1;
        while (pos + len*2 <= n) {
            double r;
            min_circle(p, pos, pos + len*2 - 1, r);
            if (r <= d + EPS) len *= 2;
            else break;
        }
        // 二分找最大合法长度
        int L = len, R = len*2;
        int best = len;
        while (L <= R) {
            int mid = (L + R)/2;
            if (pos + mid > n) {
                R = mid - 1;
                continue;
            }
            double r;
            min_circle(p, pos, pos + mid - 1, r);
            if (r <= d + EPS) {
                best = mid;
                L = mid + 1;
            } else {
                R = mid - 1;
            }
        }
        seg_end.push_back(pos + best - 1);
        pos += best;
    }
    return cnt <= m;
}

int main() {
    srand(time(0));
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    p.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> p[i].x >> p[i].y;
    }
    // 二分答案
    double l = 0, r = 2e7;
    for (int iter = 0; iter < 50; ++iter) {
        double mid = (l + r)/2;
        if (check(mid)) r = mid;
        else l = mid;
    }
    // 再跑一次check获取分段
    check(r);
    // 输出
    cout << fixed << setprecision(8) << r << "\n";
    cout << seg_end.size() << "\n";
    int pre = 0;
    for (int i = 0; i < seg_end.size(); ++i) {
        double rad;
        Point O = min_circle(p, pre, seg_end[i], rad);
        cout << fixed << setprecision(8) << O.x << " " << O.y << "\n";
        pre = seg_end[i] + 1;
    }
    return 0;
}

可不可以帮我把代码改对,题目为:WYK-Plot

原文