跳到正文

题解

题解:CF2114D Come a Little Closer

the_Shortest_Path约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年6月15日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 2 分钟
获赞
1
题目
CF2114D

版本

  1. v1当前

形式化题意

给定一个网格,其中有 nn 个点(点不会重合),可以移动其中一个点到任意不与其它点重合的位置,使得满足包含这 nn 个点的矩形面积最小。

可以维护两个 multiset(注意不能是 set),枚举删掉每一条边时的横纵坐标极差的乘积,求其最小值。

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, x[200005], y[200005];
multiset <int> sx, sy;
signed main() {
    int T;
    cin >> T;
    while (T--) {
        sx.clear(), sy.clear(); // 别忘记多测清空
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> x[i] >> y[i], sx.insert(x[i]), sy.insert(y[i]);
        if (n == 1) { // 特判,否则 multiset 会为空
             puts("1");
            continue;
        }
        int ans = (*sx.rbegin() - *sx.begin() + 1) * (*sy.rbegin() - *sy.begin() + 1); // 记得处理不删点
        for (int i = 1; i <= n; i++) {
            sx.erase(sx.find(x[i])), sy.erase(sy.find(y[i])); // 一定要删点的下标,否则集合会删除所有的与之相同的元素
            ans = min(ans, (*sx.rbegin() - *sx.begin() + 1) * (*sy.rbegin() - *sy.begin() + 1));
            if (ans == n - 1) ans += min(*sx.rbegin() - *sx.begin(), *sy.rbegin() - *sy.begin()) + 1; // 如果矩形已经满了,则需要额外添加一行或一列。
            sx.insert(x[i]), sy.insert(y[i]);
        }
        cout << ans << endl;
    }
    return 0;
}
原文