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

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

 

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

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

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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
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=0;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[0].r);
    int res = 1;
    for(int i=1;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 0;
}

 

posted @ 2017-08-12 16:07  Draymonder  阅读(195)  评论(0编辑  收藏  举报