Windows消息队列 ——优先队列

消息队列是Windows系统的基础。对于每个进程,系统维护一个消息队列。如果在进程中有特定事件发生,如点击鼠标、文字改变等,系统将把这个消息加到队列当中。同时,如果队列不是空的,这一进程循环地从队列中按照优先级获取消息。请注意优先级值低意味着优先级高。请编辑程序模拟消息队列,将消息加到队列中以及从队列中获取消息。

输入格式:

输入首先给出正整数N(≤105),随后N行,每行给出一个指令——GETPUT,分别表示从队列中取出消息或将消息添加到队列中。如果指令是PUT,后面就有一个消息名称、以及一个正整数表示消息的优先级,此数越小表示优先级越高。消息名称是长度不超过10个字符且不含空格的字符串;题目保证队列中消息的优先级无重复,且输入至少有一个GET

输出格式:

对于每个GET指令,在一行中输出消息队列中优先级最高的消息的名称和参数。如果消息队列中没有消息,输出EMPTY QUEUE!。对于PUT指令则没有输出。

输入样例:

9
PUT msg1 5
PUT msg2 4
GET
PUT msg3 2
PUT msg4 4
GET
GET
GET
GET

输出样例:

msg2
msg3
msg4
msg1
EMPTY QUEUE!
#include<bits/stdc++.h>
using namespace std;
struct xinxi
{
	string name;
	int id;
	bool operator < (const xinxi & x1) const
	{
		return id > x1.id;//小的优先级高,从小到大排
	}
}a[110];
priority_queue<xinxi> q;
int main()
{
	int n;cin >> n;
	while (n--)
	{
		string s;
		cin >> s;
		if (s == "PUT")
		{
			string str;int t;
			cin >> str >> t;
			q.push({ str,t });
		}
		else
		{
			if (q.size())
			{
				auto x = q.top();
				cout << x.name << endl;
				q.pop();
			}
			else cout << "EMPTY QUEUE!" << endl;
		}
	}
	system("pause");
}

 

#include<bits/stdc++.h>
using namespace std;
struct xinxi
{
	string ss;
	int id;
}a[100006];
struct cmp {
	bool operator() (const xinxi &x1, const xinxi &x2) {
		return x1.id > x2.id;
	}
};
priority_queue<xinxi, vector<xinxi>, cmp> q;
int main() {
	int n;cin >> n;
	for (int i = 0;i < n;i++) {
		string str;
		cin >> str;
		if (str == "PUT") {
			cin >>a[i].ss >> a[i].id;
			q.push(a[i]);
		}
		else {
			if (q.size() != 0) {
				cout << q.top().ss << endl;
				q.pop();
			}
			else {
				cout << "EMPTY QUEUE!" << endl;
			}
		}
	}
}

 priority_queue<int> p;   //默认降序排列(大的在队首)
    priority_queue<int,vector<int>,greater<int>> p;   //升序的优先队列

    pq.push(x);    //将x推入优先队列
    //priority_queue不像queue一样有front(),back()
    //使用top()前必须用empty()判断优先队列是否为空
    pq.top();     //只能通过top()来访问队首元素
    pq.pop();     //将队首元素取出
    pq.empty();
    pq.size();

bool operator() (xinxi x1,  xinxi x2) 使用引用来提高效率,此时比较类的参数需要加上"const" 和"&"

bool operator() (const xinxi &x1, const xinxi &x2)  这个题若是上面的就超时一个点。

这题用vector会超时(因为得sort) ,这里用堆排序实现减时。 

priority_queue 底层是堆实现,在优先队列中,队首元素是优先级最高的元素,加入任何元素,堆会随时调整结构。头文件加上#include<queue> 即可

与队列不同的是,优先队列没有front() back() 函数 只能通过top()来访问队首元素。

优先队列函数内cmp与sort 的cmp函数效果相反


4月11 更

 也可以改成这样

priority_queue<xinxi> q; 

friend bool operator < (const xinxi& x1, const xinxi& x2) {
        return x1.id > x2.id;
    }  这个就放到结构体里面

虽然我还没理解

#include<bits/stdc++.h>
using namespace std;
struct xinxi
{
	string ss;
	int id;
	friend bool operator < (const xinxi& x1, const xinxi& x2) {
		return x1.id > x2.id;
	}
}a[100006];

priority_queue<xinxi> q;
int main() {
	int n;cin >> n;
	for (int i = 0;i < n;i++) {
		string str;
		cin >> str;
		if (str == "PUT") {
			cin >> a[i].ss >> a[i].id;
			q.push(a[i]);
		}
		else {
			if (q.size() != 0) {
				cout << q.top().ss << endl;
				q.pop();
			}
			else {
				cout << "EMPTY QUEUE!" << endl;
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值