题意:
有M张椅子N个人,第i个人可以坐的椅子是小于 Li 或大于 Ri ,现在你可以加若干个椅子(可以视为在任意位子加,但不影响原有的椅子),使得N个人都能坐在椅子上。求最少要放几个椅子。
数据范围:
1 ≤ N, M ≤ 2×10e5
0 ≤ Li < Ri ≤ M + 1(1 ≤ i ≤ N)
算法:
把人按
L
排序,用堆维护暂时还没有满足的R
。枚举椅子,指针扫过去就行。
代码:
#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;
}