51nod 1428 活动安排问题 (贪心+优先队列)

时间:2023-03-09 13:22:41
51nod 1428 活动安排问题 (贪心+优先队列)

来源:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428

首先按照开始时间从小到大排序.

其实只要维护一个结束时间的最小堆,每次比较开始时间和堆中最小时间的大小,如果比它大就放入堆中并且时间就要变成当前任务的结束时间,

否则就要新开一个教室.并且把结束时间加入堆中,注意判断堆是否为空.

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+;
struct node{
int l,r;
bool operator<(const node & a)const
{
return l < a.l;
}
}s[maxn]; int main ()
{
int n;scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d %d",&s[i].l,&s[i].r);
priority_queue <int,vector<int>,greater<int> >Q;
sort(s,s+n);
Q.push(s[].r);
int res = ;
for(int i=;i<n;i++){
if(!Q.empty()){
int now = Q.top();
if(now > s[i].l)
{
res++;
Q.push(s[i].r);
}
else{
Q.pop();
Q.push(s[i].r);
}
}
else{
Q.push(s[i].r);
res++;
}
}
printf("%d\n",res);
return ;
}