跳到正文

个人记录

云剪贴板

lihongru约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年9月30日
收录
2026年9月30日
更新
2026年9月30日
篇幅
约 3 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 5;
int n, ans1, ans2;
bool flag[N], af[N], bf[N];
multiset<int> s[N];

int main(){
    freopen("cards.in", "r", stdin);
    freopen("cards.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++){
        int x, y; cin >> x >> y;
        if (min(x, y) > n) continue;
        if (x == y) { flag[x] = true; continue; }
        if (max(x, y) > n) {
            if (!af[min(x, y)]) af[min(x, y)] = true;
            else bf[min(x, y)] = true;
            continue;
        } 
        s[min(x, y)].insert(max(x, y));
    }
    for (; s[ans1].size() || flag[ans1] || af[ans1]; ans1++){
        while (s[ans1].size() > 1 || s[ans1].size() && (flag[ans1] || af[ans1])){
            int x = *--s[ans1].end();
            s[x].insert(ans1);
            s[ans1].erase(--s[ans1].end());
        }
        if (s[ans1].size() && *s[ans1].begin() <= n)
            bf[*s[ans1].begin()] = true;
        s[ans1].clear(); af[ans1] = false;
    }
    for (int i = ans1; i <= n; i++){
        for (multiset<int>::iterator it = s[i].begin(); it != s[i].end(); ){
            if (*it < i) { s[*it].insert(i); it = s[i].erase(it); }
            else it++;
        }
    }
    for (; s[ans2].size() || flag[ans2] || af[ans2] || bf[ans2]; ans2++){
        while (s[ans2].size() > 1 || s[ans2].size() && (flag[ans2] || af[ans2] || bf[ans2])){
            int x = *--s[ans2].end();
            s[x].insert(ans2);
            s[ans2].erase(--s[ans2].end());
        }
    }
    cout << ans1 + ans2 << endl;
    return 0;
}
原文