【C++】牛客——活动安排

✨题目链接:

AB31 活动安排


✨题目描述 

给定𝑛个活动,每个活动安排的时间为[𝑎𝑖,𝑏𝑖)。求最多可以选择多少个活动,满足选择的活动时间两两之间没有重合。

✨输入描述:

第一行输入一个整数        𝑛         1\leq n\leq 2*10^{5},表示可选活动个数。
接下来的𝑛n行,每行输入两个整数        𝑎𝑖,𝑏𝑖      0\leq a_{i} < b_{i} \leq 10^{9},表示第  𝑖  个活动的时间。

✨输出描述:

输出一行一个整数,表示最多可选择的活动数。

✨示例


📍输入

3
1 4
1 3
3 5


📍输出

2



📍说明


✨解题思路

我们可以把时间对存在 pair<int,int>中,把 pair 放在优先级队列中
priority_queue 头文件<queue>
写一个比较 pair 的仿函数,用小根堆比较方式,把最小的放在堆顶
这样我们每次开始时间或结束时间最小的一个


✨代码
 

#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;
}


※ 如果文章对你有帮助的话,可以点赞收藏!!谢谢支持

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一岁就可帅-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值