ARC076F Exhausted

ARC076F Exhausted?

There \ (n-\) individual and \ (m \) bar bench, each bench can only take a person, the \ (I \) individuals can sit numbered \ ([1, \ l_i] \ cup [r_i, \ m] \) of the bench, ask how many people will at least not sit down.

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

Greedy, data structures


If only \ (l_i, \ r_i \) a limit of, obviously can greedy. Consider Ascending enumerate everyone \ (i \) , as far as possible on the left, if there is no position on the left side, then fill in the right already considered the minimum point on the left point and \ (i \) exchange (you can use heap maintenance). After this process is done, the remaining points greed arranged in the right spot, because this is equivalent to only \ (r_i \) restrictions, can be directly greedy.

Time complexity \ (O (n \ log n ) \)

Code

#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;
}

Guess you like

Origin www.cnblogs.com/Juanzhang/p/11327560.html