codeforces 1249D2(贪心)

传送门

题意:

给出 n n n条线段,被大于 k k k条线段覆盖的点称为坏点,求最少删除多少个线段,才能使得没有坏点。

题解:

贪心。

从左往右遍历每个点,如果该点是坏点,求出包含这个点的所有线段,然后优先删除右端点最大的线段。

为什么?因为从左往右遍历点的过程中,该坏点的左边已经没有坏点了,那么肯定优先删除往右靠的线段,因为它包含了更多的点,删除这些线段可以使得答案更优。

考虑怎么实现:利用 s e t set set维护

从左往右遍历点的过程中,利用 s e t set set 维护出包含该点的线段,如果 s e t set set内线段数量大于 k k k ,那么删除 r r r 最大的几个线段。

代码:

#pragma GCC diagnostic error "-std=c++11"
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<ctime>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
const int mod=1e9+7;
const int MAXN=2e5+5;
const int inf=0x3f3f3f3f;
set<pii>s;
std::vector<pii> v[MAXN];
std::vector<int> ans;
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        int l,r;
        scanf("%d%d",&l,&r);
        v[l].push_back({r,i});
    }

    for(int i=1;i<MAXN;i++){
        for(auto j:v[i]){
            s.insert(j);
        }
        while(s.size()&&s.begin()->first<i){
            s.erase(*s.begin());
        }
        while(s.size()>k){
            ans.push_back(s.rbegin()->second);
            s.erase(*s.rbegin());
        }
    }
    printf("%d\n",ans.size());
    for(auto j:ans){
        printf("%d ",j);
    }
}
posted @ 2021-08-04 17:16  TheBestQAQ  阅读(32)  评论(0)    收藏  举报