题解:P16965 [SCCPC 2026] 星系观测计划

· · 题解

这是本蒟蒻的第一篇题解,如有错误希望各位大佬多多指教

题解:P16965 星系观测计划

题意简述

给定平面上 n 个点,它们绕原点 O(0,0) 以相同角速度匀速旋转。定义任意时刻的最小外接轴平行矩形的周长为 P(\theta),求旋转一周内 P(\theta) 的平均值。

思路分析

凸包

最小外接矩形的大小只由最外层的点决定,内部点不会影响边界。因此先求这些点的凸包,之后所有计算只针对凸包顶点。

柯西公式

对于一个凸图形,它在所有方向上的投影长度的平均值等于:

\frac{\text{周长}}{\pi}

推导答案

设凸包周长为 C。

轴平行矩形的周长为:

P(\theta) = 2 \times (\text{X方向跨度} + \text{Y方向跨度})

由于旋转角 \theta 均匀分布,X方向和Y方向的平均跨度相等,都等于投影平均宽度,即 \dfrac{C}{\pi}。

所以矩形周长的平均值为:

\overline{P} = 2 \times \left(\frac{C}{\pi} + \frac{C}{\pi}\right) = \frac{4C}{\pi}

最终答案:

\boxed{\text{ans} = \frac{4 \times \text{凸包周长}}{\pi}}

特殊情况

若所有点共线,凸包退化为一条线段,周长按线段长度的两倍计算,公式仍然成立。

算法步骤

  1. 读入所有点。
  2. 使用 Andrew 算法求凸包。
  3. 计算凸包周长 C。
  4. 输出 \dfrac{4C}{\pi},保留 12 位小数。

复杂度分析

参考代码


#include <bits/stdc++.h>
using namespace std;

using ld = long double;
const ld PI = acosl(-1.0L);

struct Point {
    ld x, y;
    bool operator<(const Point& other) const {
        return x < other.x || (x == other.x && y < other.y);
    }
    bool operator==(const Point& other) const {
        return x == other.x && y == other.y;
    }
};

ld cross(const Point& a, const Point& b, const Point& c) {
    return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}

ld dist(const Point& a, const Point& b) {
    return sqrtl((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

vector<Point> convexHull(vector<Point> pts) {
    sort(pts.begin(), pts.end());
    pts.erase(unique(pts.begin(), pts.end()), pts.end());
    if (pts.size() <= 1) return pts;

    vector<Point> lower, upper;
    for (auto& p : pts) {
        while (lower.size() >= 2 && cross(lower[lower.size()-2], lower.back(), p) <= 0)
            lower.pop_back();
        lower.push_back(p);
    }
    for (int i = (int)pts.size() - 1; i >= 0; --i) {
        auto& p = pts[i];
        while (upper.size() >= 2 && cross(upper[upper.size()-2], upper.back(), p) <= 0)
            upper.pop_back();
        upper.push_back(p);
    }
    lower.pop_back();
    upper.pop_back();
    lower.insert(lower.end(), upper.begin(), upper.end());
    return lower;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    cout << fixed << setprecision(12);

    while (T--) {
        int n;
        cin >> n;
        vector<Point> pts(n);
        for (int i = 0; i < n; ++i) {
            cin >> pts[i].x >> pts[i].y;
        }

        vector<Point> hull = convexHull(pts);
        ld perimeter = 0;

        if (hull.size() == 2) {
            perimeter = 2.0L * dist(hull[0], hull[1]);
        } else {
            for (int i = 0; i < (int)hull.size(); ++i) {
                perimeter += dist(hull[i], hull[(i + 1) % hull.size()]);
            }
        }

        ld ans = 4.0L * perimeter / PI;
        cout << (double)ans << '\n';
    }

    return 0;
}