E - Entertainment Box (贪心+multiset)

补题确实很重要,之前碰到过类似用法比赛调了一阵子真出了。

首先把原序列按照节目结束时间按照升序排序,然后用multiset来存当前录像机中所录制的节目,(保存的是结束时间),然后是贪心,我们在multiset优先找到最大且小于或等于要录制节目开始时间的一个,将其取出,然后插入该节目。对于这个查找,我们可以用multiset自带的upper_bound来二分查找,(upper_bound(x)是找到第一个严格大于值,并返回其定位器),得到该位置的上一个位置即为查找目标,记得特判定位器为begin()的情况,指到它的前面会re 

ac代码

#include<iostream>
#include<cstring>
#include<queue>
#include<set>
#include<algorithm>
const int  N = 1e5 + 10;
using namespace std;
#define FAST ios::sync_with_stdio(false)
struct node
{
    int s, e;
} a[N];
bool  cmp(node a, node b)
{
    return a.e < b.e;
}
multiset<int>se;
int main()
{
    FAST;
    int ans = 0;
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)cin >> a[i].s >> a[i].e;
    sort(a + 1, a + 1 + n, cmp);
    se.insert(a[1].e);
    ans++;
    for (int i = 2; i <= n; i++)
    {
        auto t = se.upper_bound(a[i].s);
        if (t != se.begin())
        {
            t--;
            if (*t <= a[i].s)
            {
                se.erase(t);
                se.insert(a[i].e);
                ans++;
            }
            else
            {
                if (se.size() == m)continue;
                se.insert(a[i].e);
                ans++;
            }
        }
        else
        {
            if (se.size() == m)continue;
            se.insert(a[i].e);
            ans++;
        }
    }
    cout << ans << endl;
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值