题目链接
贪心思路:
区间左边界从小到大排序,遍历每个用户,将每个用户放在最小的右边界区间中,把右边界和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;
}
}