H - Windows Message Queue

Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue. Meanwhile, the process will do a loop for getting message from the queue according to the priority value if it is not empty. Note that the less priority value means the higher priority. In this problem, you are asked to simulate the message queue for putting messages to and getting message from the message queue.

Input

There's only one test case in the input. Each line is a command, "GET" or "PUT", which means getting message or putting message. If the command is "PUT", there're one string means the message name and two integer means the parameter and priority followed by. There will be at most 60000 command. Note that one message can appear twice or more and if two messages have the same priority, the one comes first will be processed first.(i.e., FIFO for the same priority.) Process to the end-of-file.

Output

For each "GET" command, output the command getting from the message queue with the name and parameter in one line. If there's no message in the queue, output "EMPTY QUEUE!". There's no output for "PUT" command.

Sample Input

GET
PUT msg1 10 5
PUT msg2 10 4
GET
GET
GET

Sample Output

EMPTY QUEUE!
msg2 10
msg1 10
EMPTY QUEUE!

按优先度,然后相同的FIFO,不能用vector然后自己排,复杂度太高会T,只能用优先队列;

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h> 
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e9+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
struct pp
{
	char name[20];
	int num,lev,id;
 	friend bool operator <(pp a,pp b)
	 {
	 	if(a.lev ==b.lev)
	 	return a.id >b.id ;//要加个这个,符合FIFO,不然会错
	 	return a.lev >b.lev;
	  } 
};
priority_queue<pp>v;
void Get()
{
	if(v.empty()) pf("EMPTY QUEUE!\n");
	else{
		pp t=v.top();
		pf("%s %d\n",t.name,t.num);
		v.pop();	
	}
}
int main()
{
	int ans=1;
	while(!v.empty()) v.pop();
	char q[5];
	while(~sf("%s",q))
	{
		if(q[0]=='G')
		Get();
		else
		{
			pp t;
			sf("%s%d%d",t.name,&t.num,&t.lev);
			t.id =ans++;
			v.push(t); 
		}
	}
	return 0;
}
posted @ 2018-08-02 16:51  一无所知小白龙  阅读(315)  评论(0编辑  收藏  举报