zoukankan      html  css  js  c++  java
  • 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
    ",res);
        return 0;
    }
  • 相关阅读:
    未解决的
    nodejs 7 和 8 的比较
    openresty Nginx
    Vim快捷键分类
    wireshark 包过滤
    RSA 公私钥 互换问题
    vim命令
    Windows 小端存储
    python 字符转换
    ssl证书验证
  • 原文地址:https://www.cnblogs.com/Draymonder/p/7350605.html
Copyright © 2011-2022 走看看