模拟窗口消息队列

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!







===============================================


算法:(主要是优先队列的用法:

1.输入命令;

2.判断是GET还是PUT

3.若是PUT,则输入str,par,pri,按pri大小升序,若pri相同,则根据接受的先后顺序(计数),

存入优先队列

4.若是GET,则判断优先队列中是否数据

                   a.优先队列不为空:输出最底端数据的str和par (先进先出

                   b.优先队列为空,输出空。

5.返回1,直到不再输入命令



====================================



#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

struct node
{
	char str[100];
	int a, b, t;
	friend bool operator <(node x, node y)
	{
		if (x.b != y.b) return x.b > y.b;
		return x.t > y.t;
	}
};
node p;
priority_queue<node> s;

int main()
{
	char com[10];
	int a, b, count = 0;
	while (~scanf("%s", com))
	{
		if (strcmp(com,"GET")==0)
		{
			if (!s.empty())
			{
				printf("%s %d\n", s.top().str,s.top().a);
				s.pop();
			}
			else printf("EMPTY QUEUE!\n");

		}
		else
		{
			scanf("%s %d%d", p.str, &p.a, &p.b);
			p.t = ++count;
			s.push(p);
		}
	}
	return 0;
}




要求: 客户业务分为两种。第一种是申请从银行得到一笔资金,即取款或借款。第二种是向银行投入一笔资金,即存款或还款。银行有两个服务窗口,相应地有两个队列。客户到达银行后先排第一个队。处理每个客户业务时,如果居于第一种,且申请额超出银行现存资金总额顺得不到满足,则立刻排入第二个队等候,直至满足时才离开银行;否则业务处理完后立刻离开银行。每接待完一个第二种业务的客户,则顺序检查相处理(如果可能)第二个队列中的客广,对能满足的申请者予以满足,不能满足者重新排列第二个队列的队尾。注意,在此检查过程中,一旦银行资金总额少于或等于刚才第一个队列中最后一个客户(第二种业务)被接待之前的数额,或者本次已将第二个队列检查或处理了一遍,就停止被盗(因为此时已不可能还有能满足者)转而继续接待第一个队列的客户。任何时刻都只开一个窗口。假设检查不需要时间。营业时间结束时所有存户立即离开银行。 写一个上述银行业务的事件驱动模拟系统,通过模拟方法求出客户在银行内逗留的平 均时间。 [测试数据] 一天营业开始时银行拥有的款额为10000(元).营业时间为600(分钟)。其他模拟参量 自定。注意测定两种极端的情况:一是两个到达事件之间的间隔时间很短,而客户的交易时 间很长,另一个恰好相反,设置两个到达事件的间隔时间很长,而客户的交易时间很短。 [实现提示] 事件有两类;到达银行和离开银行。韧始时银行现存资金总额为total。开始营业后的第 —个事件是客户到达,营业时间从0到closetime。到达事件发生时随机地设置此客户的交 易时间相距下一到达事件之间的时间间隔。每个客户要办理的款额也是随机确定的,用负值 和正值分别表示第一类相第二类业务。变量total、closetime以及上述两个随机量的上下界 均文互地从终端读入,作为模拟参数。 两个队列和一个事件表均要用动态存储结构实现。注意弄清应该在什么条件下设置离开事件,以及第二个队列甩怎样的存储结构实现时可以获得较高的效率。注意:事件表是按 时间顺序有序的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值