跳到正文

题解

题解:P6940 [ICPC 2017 WF] Visual Python++

Adam1101约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年10月11日
收录
2026年10月30日
更新
2026年11月4日
篇幅
约 11 分钟
获赞
7
题目
P6940

版本

  1. v1当前

提供一种比较好写的 set 做法。

题意简述

给定 nn 个左上角和 nn 个右下角,需要将左上角和右下角一一匹配使得匹配出的 nn 个矩形要么包含要么不相交。

只要有边界重合就不合法。

思路

先思考如何最优匹配,再想怎么判断是否合法。

匹配

从左往右扫每一个角,对于一个右下角,尝试匹配还未匹配的左上角中,行坐标小于等于当前右下角行坐标的最大者。

说着有点绕,画个图就知道了。

代码
    ╔

 ╔
         ╝

对于这样的一个右下角,有两个未匹配的左上角,我们选择下面那个。

如果我们选择上面那个,那么下面那个就一定不合法了,因为后面的右下角匹配后无论如何都会与这个矩形有交。

反之,如果选择下面这个,可能有这样的情况让它合法:

代码
    ╔
             ╝
 ╔
         ╝

所以选下面这个左上角匹配一定不劣。

这个匹配的过程可以用一个 set 维护,代码如下:

匹配
C++
struct Dot {
    int x, y;
    bool type;  // false: left, true: right
    int id;

    friend bool operator<(const Dot& x, const Dot& y) {
        if (x.y == y.y) return x.x < y.x;
        return x.y < y.y;
    }
} a[N << 1];
int ans[N];

struct Cmp {
    bool operator()(const Dot& x, const Dot& y) const {
        if (x.x == y.x) return x.y < y.y;
        return x.x < y.x;
    }
};
set<Dot, Cmp> s;  // 存储所有左上角,按行坐标排序

int main() {
    /*省略输入输出*/

    sort(a + 1, a + (n << 1) + 1);

    for (int i = 1; i <= (n << 1); ++i) {
        if (!a[i].type) {
            s.insert(a[i]);
            continue;
        }
        auto it = s.upper_bound({a[i].x, INF});   // 寻找行坐标不大于当前右下角行坐标的最大左上角
        if (it == s.begin()) return cout << "syntax error\n", 0;  // 如果没有左上角直接格式错误
        --it;
        Dot l = *it;

        ans[l.id] = i;
        s.erase(it);
    }
}

判断

人类智慧做法(非正解,仅供娱乐,不推荐)

我们充分发扬人类智慧:

对所有矩形按其左上角的 xx 坐标排序。

根据数学直觉,在排序后,可能发生交集的矩形在数组中肯定不会离得太远。

所以我们只取每个点向后的 1515 个矩形来判断。

这样速度快得飞起,在 n=100000n = 100000 时都可以在 74ms 内卡过。

考虑有哪些不合法的情况:

代码
╔═══╗       ╔═══╗   ╔══════╗     ╔══╗
║ ╔═╬═╗   ╔═╬═╗ ║   ║ ╔══╗ ║   ╔═╬══╬═╗
╚═╬═╝ ║   ║ ╚═╬═╝   ╚═╬══╬═╝   ║ ╚══╝ ║
  ╚═══╝   ╚═══╝       ╚══╝     ╚══════╝
╔═══╗       ╔═══╗     ╔═╗
║ ╔═╬═╗   ╔═╬═╗ ║   ╔═╬═╬═╗
║ ╚═╬═╝   ╚═╬═╝ ║   ╚═╬═╬═╝
╚═══╝       ╚═══╝     ╚═╝

稍稍思考就会发现第一、五、六、七种在我们的贪心匹配中不可能出现。

我们想象一个类似一维括号匹配的思路:

从左往右扫描每个矩形,扫到左边界就加入一个 multiset,扫到右边界就从中删除。

这样就保证了 multiset 中所有的矩形都还是“未结束”的,像这样:

代码
╔════
║   ╔
║   ╚
║ ╔══
╚═╬══
  ╚══

发现无论是哪种不合法的情况,都等价于当前矩形的“起始边”被另一个“未结束”的矩形的上下边界“穿过”。

所以只需要在 multiset 中存下“未结束”的矩形的上下边界,每次加入矩形时判断当前矩形的上下边界之间是否有别的“未结束”的矩形的上下边界就行了。

但题目中说了,边界重合的情况也不合法:

代码
╔═╦═╦═╗
║ ║ ║ ║
╚═╩═╩═╝

这种可以不用管,我们的做法会直接判掉。

代码
╔═══╗
╠═══╣
╠═══╣
╚═══╝

对于这种,我们可以在扫描时同列先扫高度(行坐标之差)更小的,这样扫到大的时就可以判断到小的了(具体可以见代码)。

但还有一种非常神奇的情况:

代码
╔═══╗
║  (║)-> 这里是一个宽为 1 的矩形
╚═══╝

我们的代码会加入大的、加入小的,删除小的,再删除大的,根本没有判断它是否合法。

解决方法:可以竖着从上往下再进行一遍扫描,这样就能判掉这种情况。

复杂度分析

匹配时扫一遍所有的角,set 插入删除 O(log⁡n)O(\log n),总共 O(nlog⁡n)O(n \log n)。

判断时扫一遍所有矩形,同样 multiset 插入删除,复杂度也是 O(nlog⁡n)O(n \log n)。

总时间复杂度 O(nlog⁡n)O(n \log n),空间复杂度线性。

代码实现

AC 代码
C++
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e5 + 10, INF = 0x3f3f3f3f;

struct Dot {
    int x, y;
    bool type;  // false: left, true: right
    int id;

    friend bool operator<(const Dot& x, const Dot& y) {
        if (x.y == y.y) return x.x < y.x;
        return x.y < y.y;
    }
} a[N << 1];
int ans[N];

struct Cmp {
    bool operator()(const Dot& x, const Dot& y) const {
        if (x.x == y.x) return x.y < y.y;
        return x.x < y.x;
    }
};
set<Dot, Cmp> s;

struct Line {
    int l, r, k;
    bool type;  // false: left, true: right

    friend bool operator<(const Line& x, const Line& y) {
        if (x.k != y.k) return x.k < y.k;
        if (x.r - x.l != y.r - y.l) return (x.r - x.l) < (y.r - y.l);  // 小的放前面
        return x.type < y.type;  // 优先放加入的,避免删除一个未被加入的数
    }
} l1[N << 1], l2[N << 1];

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

    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        int x, y;
        cin >> x >> y;
        a[i] = {x, y, false, i};
    }
    for (int i = 1; i <= n; ++i) {
        int x, y;
        cin >> x >> y;
        a[n + i] = {x, y, true, i};
    }

    sort(a + 1, a + (n << 1) + 1);

    for (int i = 1; i <= (n << 1); ++i) {  // 匹配
        if (!a[i].type) {
            s.insert(a[i]);
            continue;
        }
        auto it = s.upper_bound({a[i].x, INF});
        if (it == s.begin()) return cout << "syntax error\n", 0;
        --it;
        Dot l = *it;

        ans[l.id] = i;
        s.erase(it);
    }

    int cur1 = 0, cur2 = 0;
    for (int i = 1; i <= (n << 1); ++i) {
        if (a[i].type) continue;
        l1[++cur1] = {a[i].x, a[ans[a[i].id]].x, a[i].y, false};
        l1[++cur1] = {a[i].x, a[ans[a[i].id]].x, a[ans[a[i].id]].y, true};  // 从左往右横着

        l2[++cur2] = {a[i].y, a[ans[a[i].id]].y, a[i].x, false};
        l2[++cur2] = {a[i].y, a[ans[a[i].id]].y, a[ans[a[i].id]].x, true};  // 从上往下竖着
    }
    sort(l1 + 1, l1 + (n << 1) + 1);
    sort(l2 + 1, l2 + (n << 1) + 1);

    multiset<int> ms1, ms2;  // 可能有重复
    for (int i = 1; i <= (n << 1); ++i) {
        if (!l1[i].type) {
            auto it1 = ms1.lower_bound(l1[i].l);  // 第一个 >= l
            auto it2 = ms1.upper_bound(l1[i].r);  // 第一个 > r
            if (it1 != it2) return cout << "syntax error\n", 0;  // 不相等说明区间内被别的穿过

            ms1.insert(l1[i].l);  // 加入
            ms1.insert(l1[i].r);
        } else {
            ms1.erase(ms1.find(l1[i].l));  // erase(key) 会删掉所有 key
            ms1.erase(ms1.find(l1[i].r));  // erase(find(key)) 只会删掉一个
        }

        if (!l2[i].type) {  // 同上
            auto it1 = ms2.lower_bound(l2[i].l);
            auto it2 = ms2.upper_bound(l2[i].r);
            if (it1 != it2) return cout << "syntax error\n", 0;

            ms2.insert(l2[i].l);
            ms2.insert(l2[i].r);
        } else {
            ms2.erase(ms2.find(l2[i].l));
            ms2.erase(ms2.find(l2[i].r));
        }
    }

    for (int i = 1; i <= n; ++i) cout << a[ans[i]].id << '\n';
}
原文
存档评论2 条抓取于