7-20 Windows消息队列 (25 分)(模拟水题)

本文介绍了一种使用优先队列(最小堆)进行任务调度的模拟实现方法,通过C++代码示例展示了如何利用优先队列对不同优先级的任务进行管理,确保高优先级任务能够被优先处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意: 

思路: 用优先队列直接模拟就OK了,另外优先队列存pair的时候比较的是first的值,实测!!

上代码:

#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#define INF 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin)

using namespace std;
typedef long long ll;
typedef pair<int,string> P;
const int maxn = 1e5+10;
priority_queue<P, vector<P>, greater<P> > que;
string op,name;
int n,id;

int main() {
    //FRE();
    cin>>n;
    for(int i = 0; i<n; i++) {
        cin>>op;
        if(op[0]=='P') {
            cin>>name>>id;
            que.push(P(id, name));
        } else {
            if(que.empty()){
                cout<<"EMPTY QUEUE!"<<endl;
            }
            else{
                P p = que.top();
                que.pop();
                cout<<p.second<<endl;
            }
        }
    }
    return 0;
}
/*
样例输入:
9
PUT msg1 5
PUT msg2 4
GET
PUT msg3 2
PUT msg4 4
GET
GET
GET
GET
样例输出:
msg2
msg3
msg4
msg1
EMPTY QUEUE!
*/

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值