补题:Codeforces Round #595 (Div. 3)

比赛入口

D2 Too Many Segments (hard version)

做法:题意是最少删除那几个区间使得每个点被不多于k个区间覆盖。做法是很简单的贪心…(雾)维护一个set,从左到右遍历每个点,优先删除过当前点右端点较远的区间…

代码

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int N = 2e5+5;
vector<pii> ve[N];
vector<int> ans;
set<pii> st;
int main() {
  int n, k;
  read(n); read(k);
  for(int i = 1, u, v; i <= n; ++i) {
    read(u); read(v);
    ve[u].push_back(make_pair(v, i));
  }
  for(int i = 0;i < N; ++i) {
    for(auto it : ve[i]) st.insert(it);
    while(st.size() && st.begin()->first < i) st.erase(st.begin());
    while(st.size() > k) {
      ans.push_back(st.rbegin()->second);
      st.erase(*st.rbegin());
    }
  }
  cout << ans.size() << endl;
  for(int x : ans) cout << x << " ";
  cout << endl;
  return 0;
}

发布了28 篇原创文章 · 获赞 14 · 访问量 2954

猜你喜欢

转载自blog.csdn.net/qq_43408978/article/details/104080529