ARC 076 F - Exhausted? -堆

题意:

有M张椅子N个人,第i个人可以坐的椅子是小于 Li 或大于 Ri ,现在你可以加若干个椅子(可以视为在任意位子加,但不影响原有的椅子),使得N个人都能坐在椅子上。求最少要放几个椅子。

数据范围:

1 ≤ N, M ≤ 2×10e5
0 ≤ Li < Ri ≤ M + 1(1 ≤ i ≤ N)

算法:

把人按排序,用堆维护暂时还没有满足的。枚举椅子,指针扫过去就行。

代码:
#include <bits/stdc++.h>

/*
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <set>
*/

using namespace std;

typedef long long LL;
typedef double DB;
typedef unsigned int UI;
typedef pair<int, int> PII;

const int inf = 0x7f7f7f7f;

#define rdi() read<int>()
#define rdl() read<LL>()
#define rds(a) scanf("%s", a)
#define mk(i, j) make_pair(i, j)
#define pb push_back
#define fi first
#define se second
#define For(i, j, k) for (int i = j; i <= k; i ++)
#define Rep(i, j, k) for (int i = j; i >= k; i --)
#define Edge(i, u) for (int i = head[u]; i; i = e[i].nxt)

template<typename t> t read() {
    t x = 0;
    int f = 1;
    char c = getchar();
    while (c > '9' || c < '0') f = c == '-' ? -1 : 1 , c = getchar();
    while (c >= '0' && c <= '9') x = x * 10 + c - 48 , c = getchar();
    return x * f;
}

template<typename t> void write(t x) {
    if (x < 0) {
        putchar('-'), write(-x);
        return;
    }
    if (x >= 10) write(x / 10);
    putchar(x % 10 + 48);
}

#define N 200010
struct aa {
    int l, r;
} a[N];
int n, m, i, ans, now;

bool cmp(aa a, aa b) {
    if (a.l == b.l) return a.r > b.r;
    return a.l < b.l;
}

struct bb {
    int val;
    bool operator < (const bb &t) const{
        return val > t.val;
    }
};

priority_queue < bb > Heap;

int main() {
    n = rdi(), m = rdi();
    for (i = 1; i <= n; i++) a[i].l = rdi(), a[i].r = rdi();
    sort(a + 1, a + n + 1, cmp);
    for (now = 1; now <= n; now++) if (a[now].l) break;
    now--;
    for (i = 1; i <= now; i++) Heap.push((bb){a[1].r});
    for (i = 1; i <= m; i++) {
        while (now < n && a[now + 1].l < i) now++, Heap.push((bb){a[now].r});
        if (now < n) {
            now++;
            if (++ans == n) break;
            continue;
        }
        if (!Heap.empty() && Heap.top().val <= i) {
            Heap.pop();
            if (++ans == n) break;
            continue;
        }
    }
    printf("%d\n", n - ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值