网络优化----贪心+堆

题目链接在这里插入图片描述

在这里插入图片描述
贪心思路:
区间左边界从小到大排序,遍历每个用户,将每个用户放在最小的右边界区间中,把右边界和v的值作为堆的键值

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int N = 10010;
//#define int long long
int n,m;
typedef pair<int,int>PII;
struct node
{
    int l,r,v;
    bool operator < (const node &W)const
    {
        if(l!=W.l)return l<W.l;
        if(r!=W.r)return r<W.r;
        return v<W.v;
    }
}a[N];
signed main()
{
    while(cin>>n>>m)
    {
        for(int i=1;i<=m;i++)cin>>a[i].l>>a[i].r>>a[i].v;
        sort(a+1,a+m+1);
        priority_queue<PII,vector<PII>,greater<>> heap;
        int res=0;
        for(int i=1,k=1;i<=n;i++)
        {
            while(a[k].l==i)heap.push({a[k].r,a[k].v}),k++;
            while(heap.size()&&(i>heap.top().first||heap.top().second==0))heap.pop();
            if(heap.empty())continue;
            auto t=heap.top();
            heap.pop();
            heap.push({t.first,t.second-1});
            res++;
        }
        cout<<res<<endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_WAWA鱼_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值