ARC076F Exhausted

ARC076F Exhausted?

\(n\) 个人和 \(m\) 条板凳,每个板凳只能坐一个人,第 \(i\) 个人可以坐在编号为 \([1,\ l_i]\cup[r_i,\ m]\) 的板凳,求最少会有多少个人不能坐下。

\(n,\ m\leq2\times10^5,\ 0\leq l_i<r_i\leq m+1\)

贪心,数据结构


如果只有 \(l_i,\ r_i\) 中的一个限制,显然可以贪心。考虑升序枚举每一个人 \(i\) ,尽量安排在左侧,若左侧没有位置了,那么考虑将已经填在左侧的右端点最小的点和 \(i\) 交换(可以用堆维护)。这个过程做完之后,将剩下的点贪心安排在右端点,因为这相当于只剩 \(r_i\) 的限制,可以直接贪心。

时间复杂度 \(O(n\log n)\)

代码

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

const int maxn = 2e5 + 10;
int n, m, tot, data[maxn];

struct node {
  int l, r;
  
  inline bool operator < (const node &o) const {
    return l < o.l || (l == o.l && r < o.r);
  }
} a[maxn];

priority_queue <int, vector <int>, greater <int> > Q;

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &a[i].l, &a[i].r);
  }
  int L = 1, R = m;
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    Q.push(a[i].r);
    if (L <= R && L <= a[i].l) {
      L++;
    } else {
      data[++tot] = Q.top(), Q.pop();
    }
  }
  int ans = 0;
  sort(data + 1, data + tot + 1);
  for (int i = tot; i; i--) {
    L <= R && R >= data[i] ? R-- : ans++;
  }
  printf("%d", ans);
  return 0;
}
posted @ 2019-08-09 15:36  cnJuanzhang  阅读(129)  评论(0编辑  收藏  举报