杭电 1509 Windows Message Queue 队列 附题目翻译

Windows Message Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4392    Accepted Submission(s): 1744


Problem Description
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!

 

问题描述

消息队列是windows系统的基本(basic fundamental)。对于每个程序,系统都会有一个消息队列。如果这个程序被操作,比如鼠标点击,文本(text)修改,系统将会给队列发送消息。与此同时,在非空的情况下,程序将会根据优先级值(priority value)从队列中不断得到消息。注意,优先级值越低表示的优先级越高。在这个问题中,你要模仿接收消息和发送消息的队列。

 

输入

输入项中只有一种情况。每一行是一个指令,接受或发送,即接收消息和发送消息。如果指令是发送,则会有一个表示消息名称的字符串还有两个整数,一个表示参数(parameter),一个表示优先权(priority)最多会有60000条指令。注意,一条消息能够出现两次或者更多,如果两条消息具有同样的优先权,那么先接收的那条将被执行(IE,FIFO都是这样的)。直至文件结束。

 

输出

对于每条接受指令,从消息队列中输出一行消息名和参数。如果在队列中没有消息,那么输出空队列。对于接受指令则没有输出。

 

算是我做的第一道队列题吧,其实不难,主要练得就是输入输出和排序。不过要注意以下几点:

1.由于编译器等原因的不同,有的要加friend。

2.编号的时候直接++i就可以了,后面不用考虑想减了,不知道为什么不减就不错,可能i是用来统计输入的总和的吧。

 

#include
#include
#include
using namespace std;

struct node{
	char name[110];
	int num,id,no;
	friend bool operator < (node a,node b){
		if(a.no!=b.no) return a.no>b.no;
		return a.id>b.id;
	}
};
priority_queue  q;

int main(){
	char a[110];
	int i=0;
	while(~scanf("%s",a)){
		node temp;
		if(strcmp(a,"GET")==0){
			if(!q.empty())
			{
				printf("%s %d\n",q.top().name,q.top().num);
				q.pop();
			}
			else{
				printf("EMPTY QUEUE!\n");
			}
		}
		else{
			scanf("%s %d %d",temp.name,&temp.num,&temp.no);
			temp.id=++i;
			q.push(temp);
		}
	}
	return 0;
}

你可能感兴趣的:(c语言,HDOJ,队列)