廊桥分配问题

这是2021csp-s复赛的第2题一开始用栈只对了一个所以考虑到优先队列

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

typedef pair<int, int> pii;

struct range {
    int x, y;
} a[100005], b[100005];

int res1[100005], res2[100005];
int n;

bool cmp(const range& a, const range& b) { 
	return a.x < b.x; 
}

int main() {
    int m1, m2;
    cin >> n >> m1 >> m2;

    for (int i = 1; i <= m1; i++) {
        cin >> a[i].x >> a[i].y;
    }

    for (int i = 1; i <= m2; i++) {
        cin >> b[i].x >> b[i].y;
    }

    sort(a + 1, a + m1 + 1, cmp);
    sort(b + 1, b + m2 + 1, cmp);

    stack<pii> lq1, lq2;
    stack<int> wq1, wq2;

    for (int i = 1; i <= n; i++) {
        wq1.push(i);
        wq2.push(i);
    }

    for (int i = 1; i <= m1; i++) {
        while (!lq1.empty() && a[i].x >= lq1.top().first) {
            wq1.push(lq1.top().second);
            lq1.pop();
        }

        if (!wq1.empty()) {
            int dest = wq1.top();
            wq1.pop();
            res1[dest]++;
            lq1.push(make_pair(a[i].y, dest));
        }
    }

    for (int i = 1; i <= m2; i++) {
        while (!lq2.empty() && b[i].x >= lq2.top().first) {
            wq2.push(lq2.top().second);
            lq2.pop();
        }

        if (!wq2.empty()) {
            int dest = wq2.top();
            wq2.pop();
            res2[dest]++;
            lq2.push(make_pair(b[i].y, dest));
        }
    }

    for (int i = 1; i <= n; i++) {
        res1[i] += res1[i - 1];
        res2[i] += res2[i - 1];
    }

    int ans = 0;
    for (int i = 0; i <= n; i++) {
        ans = max(ans, res1[i] + res2[n - i]);
    }

    cout << ans << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值