Too Many Segments (hard version) 贪心+set使用

本文解析了Codeforces竞赛中D2题目,介绍了一种通过遍历每个点并删除覆盖次数超过k的线段的最优解法。使用C++实现,涉及数据结构如vector和set。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接 ---> https://codeforces.com/contest/1249/problem/D2

大致题意:给n条线段,每条线段都覆盖他们所包含的点。问最少删除掉几条线段以至所有点中没有被覆盖的次数大于k的点

题解:遍历每个点,如果某个点被覆盖次数大于k了,那么便将覆盖该点的最长的多余条数删去即可。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
const int mx=2e5+20;
struct node{
    int y, id;
}ans[mx];
vector<node>v[mx];
set<node>s;
bool operator < (node a, node b){
    if(a.y == b.y)
        return a.id < b.id;
    return a.y < b.y;
}
int a[mx];
int main(){
    int n, k;
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++){
        node t;
        int a, b;
        scanf("%d%d",&a,&b);
        t.id = i;
        t.y = b;
        v[a].push_back(t);
    }
    int cnt=0;
    for(int i=1; i<mx; i++){
        while(s.size()&&(*s.begin()).y<i)
            s.erase(*s.begin());
        for(int j=0; j<v[i].size(); j++)
            s.insert(v[i][j]);
        while(s.size()>k){
            ans[++cnt] = *s.rbegin();
            s.erase(*s.rbegin());
        }
    }
    printf("%d\n",cnt);
    for(int i=1; i<=cnt; i++){
        printf("%d%c",ans[i].id,i==cnt?'\n':' ');
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值