跳到正文

题解

题解:P16223 【模板】旋转卡壳/最远点对

Ryc12345678910约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年6月11日
收录
2026年8月18日
更新
2026年10月27日
篇幅
约 7 分钟
题目
P16223

版本

  1. v1当前

P16223题解

LinkLink

差不多双倍经验吧

废话

“学过计算几何,我便考你一考,旋转卡壳的旋转卡壳有多少种读法?”

“不能读?我教给你,旋转卡壳有 24 种读法。(bushi)”

题意

在二维平面上有 nn 个点,你需要找出所有点对中距离最远的一对。

Solution

前置知识

  1. 平面向量的基础运算知识
  2. 二维凸包

做法

首先能够想到一个事情,那就是平面上最远的点对一定在这些点的凸包上。

Why?

假设现在你选了两个点,其中至少有一个点在凸包里面,那么我们可以做一条直线,穿过这个点(不穿过另一个点),然后你做关于这条直线的平行线,肯定存在一条平行线可以使这条平行线是穿过其他凸包上的点的,这个时候的答案显然优于原来的答案。

所以最远点对一定在凸包上。

所以我们先处理出凸包。处理出凸包之后能够做到一个 O(m2)O(m^2) 的暴力,其中 mm 为凸包上的点数。

考虑优化时间复杂度。

这个时候我们就要用到旋转卡壳这一个算法了。

想象现在有两条平行线,夹住了凸包。接下来你去旋转这两条平行线,使它们紧贴着凸包朝同方向旋转。然后你去遍历两条平行线与凸包的切点的距离,取最大值,就能得到直径。

事实上实际实现的时候我们会把平行线等效替代成凸包的边。

实际算法实现的过程如下:

  1. 求出凸包。
  2. 开始旋转卡壳。每次枚举一条边,并找出离它最远的点(“对踵点”),依次算出每条边的两端点到对踵点的距离,取其中的最大值。

如果你直接暴力枚举对踵点会发现时间复杂度还是 O(m2)O(m^2)。

还是无法通过这题,这个时候我们考虑一下对踵点的性质。

注意到:“对踵点”的位置在不断前移,所以每个点都最多只需遍历一遍。

Why?

利用叉积的正负性来判断距离的趋势:

假设当前枚举的边为 pipi+1→\overrightarrow{p_ip_{i+1}}。当前的点为 pkp_k,若 cross(pipi+1→,pipk+1→)>cross(pipi+1→,pi,pk→)cross(\overrightarrow{p_ip_{i+1}},\overrightarrow{p_ip_{k+1}})>cross(\overrightarrow{p_ip_{i+1}},\overrightarrow{p_i,p_k}),则说明 pk+1p_{k+1} 离 pkp_k 更远,要继续后移。

时间复杂度

凸包:O(nlog⁡n)O(n \log n)。

旋转卡壳:O(m)O(m)。

整体:O(nlog⁡n)O(n \log n)。

Code

C++
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
struct Point {
    ll x, y;
    int id;
    Point() {}
    Point(ll _x, ll _y, int _id = -1) : x(_x), y(_y), id(_id) {}
    bool operator<(const Point& b) const {
        if (x != b.x) return x < b.x;
        return y < b.y;
    }
    Point operator-(const Point& b) const {
        return Point(x - b.x, y - b.y);
    }
};
inline ll cross(const Point& a, const Point& b) {
    return a.x * b.y - a.y * b.x;
}
inline ll len2(Point a, Point b) {
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
inline vector<Point> andrew(vector<Point> pts) {
    int n = pts.size();
    if (n <= 1) return pts;
    sort(pts.begin(), pts.end());
    vector<Point> hull;
    for (int i = 0; i < n; i++) {
        while (hull.size() >= 2 && cross(hull.back() - hull[hull.size() - 2], pts[i] - hull.back()) <= 0) hull.pop_back();
        hull.push_back(pts[i]);
    }
    int lower_len = hull.size();
    for (int i = n - 2; i >= 0; i--) {
        while ((int)hull.size() > lower_len && cross(hull.back() - hull[hull.size() - 2], pts[i] - hull.back()) <= 0) hull.pop_back();
        hull.push_back(pts[i]);
    }
    hull.pop_back();
    return hull;
}
namespace Solution {
    using pii = pair<int, int>;
    int n;
    inline void solve() {
        cin >> n;
        vector<Point> pts(n);
        for (int i = 0; i < n; i++) {
            cin >> pts[i].x >> pts[i].y;
            pts[i].id = i;
        }
        vector<Point> hull = andrew(pts);
        int m = hull.size();
        if (m == 2) {
            cout << hull[0].id << " " << hull[1].id << '\n';
            return;
        }
        ll mxd = 0;
        pii ans = { hull[0].id, hull[1].id };
        hull.push_back(hull[0]);
        int j = 1;
        for (int i = 0; i < m; i++) {
            while (cross(hull[i + 1] - hull[i], hull[j + 1] - hull[i]) > cross(hull[i + 1] - hull[i], hull[j] - hull[i])) {
                j = (j + 1) % m;
            }
            ll d1 = len2(hull[i], hull[j]);
            ll d2 = len2(hull[i + 1], hull[j]);
            if (d1 > mxd) mxd = d1, ans = { hull[i].id, hull[j].id };
            if (d2 > mxd) mxd = d2, ans = { hull[i + 1].id, hull[j].id };
        }
        cout << ans.first << " " << ans.second << '\n';
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) Solution::solve();
    return 0;
}
原文