题解 | #活动安排#

活动安排

http://www.nowcoder.com/practice/16d971e9e42e4f3b9b1e2b8794796a43

贪心算法简单运用


#include<iostream>
#include<queue>
#include<vector>
using namespace std;
struct cmp{
    bool operator()(const pair<int,int> a,const pair<int,int> b){
        if(a.second > b.second)
            return a.second > b.second;
        else if(a.second == b.second)
            return a.first < b.first;
        return false;
    }
};
int main()
{
    int n,total = 0,time = 0,a,b;
    cin >> n;
    priority_queue<pair<int,int>,vector<pair<int,int>>,cmp> qu;
    while(n--){
        cin >> a >> b;
        qu.push(pair<int,int>(a,b));
    }
    while(!qu.empty()){
        auto cur = qu.top();
        qu.pop();
        if(cur.first >= time){
            total++;
            time = cur.second;
        }
    }
    cout << total;
}
全部评论

相关推荐

葬爱~冷少:我当时都是上午刷力扣,下午背八股,有活给我先别急,没活就干自己的事情
点赞 评论 收藏
分享
LemontreeN:有的兄弟有的我今天一天面了五场,4个二面一个hr面
投递字节跳动等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务