优先队列

Windows Message Queue

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
 
  
GETPUT msg1 10 5PUT msg2 10 4GETGETGET
 
Sample Output
 
  
EMPTY QUEUE!msg2 10msg1 10EMPTY QUEUE!
 
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct message
{
	string name;
	int par;
	int pri;
	int num;
};
bool operator < (const message a,const message b)
{
	if(a.pri==b.pri) return a.num>b.num;
	else return a.pri>b.pri;
}
int main()
{
	string s;
	priority_queue<message> mes;
	message cur,next;
	int k=1;
	while(cin>>s)
	{
		if(s=="GET")
		{
			if(mes.empty()) cout<<"EMPTY QUEUE!"<<endl;
			else
			{
				cur=mes.top();
				cout<<cur.name<<" "<<cur.par<<endl;
				mes.pop();
			}
		 } 
		else
		{
			next.num=k++;
			cin>>next.name>>next.par>>next.pri;
			mes.push(next);
		}
	}
	return 0;
}

看病要排队

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12457    Accepted Submission(s): 5240


Problem Description
看病要排队这个是地球人都知道的常识。
不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个医生(汗,这么少)同时看病。而看病的人病情有轻重,所以不能根据简单的先来先服务的原则。所以医院对每种病情规定了10种不同的优先级。级别为10的优先权最高,级别为1的优先权最低。医生在看病时,则会在他的队伍里面选择一个优先权最高的人进行诊治。如果遇到两个优先权一样的病人的话,则选择最早来排队的病人。

现在就请你帮助医院模拟这个看病过程。
 

Input
输入数据包含多组测试,请处理到文件结束。
每组数据第一行有一个正整数N(0<N<2000)表示发生事件的数目。
接下来有N行分别表示发生的事件。
一共有两种事件:
1:"IN A B",表示有一个拥有优先级B的病人要求医生A诊治。(0<A<=3,0<B<=10)
2:"OUT A",表示医生A进行了一次诊治,诊治完毕后,病人出院。(0<A<=3)
 

Output
对于每个"OUT A"事件,请在一行里面输出被诊治人的编号ID。如果该事件时无病人需要诊治,则输出"EMPTY"。
诊治人的编号ID的定义为:在一组测试中,"IN A B"事件发生第K次时,进来的病人ID即为K。从1开始编号。
 

Sample Input
 
       
7IN 1 1IN 1 2OUT 1OUT 2IN 2 1OUT 2OUT 12IN 1 1OUT 1
 
Sample Output
 
       
2EMPTY311
 
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct ill
{
	int pri;
	int num;
};
bool operator < (const ill a,const ill b)
{
	if(a.pri==b.pri) return a.num>b.num;
	else return a.pri<b.pri;
}
int main()
{
	int n;
	while(cin>>n)
	{
		priority_queue<ill> d1;
		priority_queue<ill> d2;
		priority_queue<ill> d3;
		string s;
		ill cur,next;
		int i,j,k=1;
		for(i=0;i<n;i++)
		{
			cin>>s;
			if(s=="IN")
			{
				cin>>j;
				if(j==1)
				{
					cin>>cur.pri;
					cur.num=k++;
					d1.push(cur);
				}
				else if(j==2)
				{
					cin>>cur.pri;
					cur.num=k++;
					d2.push(cur);
				}
				if(j==3)
				{
					cin>>cur.pri;
					cur.num=k++;
					d3.push(cur);
				}
			}
			else
			{
				cin>>j;
				if(j==1)
				{
					if(d1.empty()) cout<<"EMPTY"<<endl;
					else
					{
						cur=d1.top();
						cout<<cur.num<<endl;
						d1.pop();
					}
				}
				if(j==2)
				{
					if(d2.empty()) cout<<"EMPTY"<<endl;
					else
					{
						cur=d2.top();
						cout<<cur.num<<endl;
						d2.pop();
					}
				}
				if(j==3)
				{
					if(d3.empty()) cout<<"EMPTY"<<endl;
					else
					{
						cur=d3.top();
						cout<<cur.num<<endl;
						d3.pop();
					}
				}
			}
		}
	}
	return 0;
}

下面是发现可以定义为数组:

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct ill
{
	int pri;
	int num;
};
bool operator < (const ill a,const ill b)
{
	if(a.pri==b.pri) return a.num>b.num;
	else return a.pri<b.pri;
}
int main()
{
	int n;
	while(cin>>n)
	{
		priority_queue<ill> d[4];
		string s;
		ill cur,next;
		int i,j,k=1;
		for(i=0;i<n;i++)
		{
			cin>>s;
			if(s=="IN")
			{
				cin>>j;
				cin>>cur.pri;
				cur.num=k++;
				d[j].push(cur);
			}
			else
			{
				cin>>j;
				if(d[j].empty()) cout<<"EMPTY"<<endl;
				else
				{
					cur=d[j].top();
					cout<<cur.num<<endl;
					d[j].pop();
				}
			}
		}
	}
	return 0;
}

寻找大富翁

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8066    Accepted Submission(s): 2944


Problem Description
浙江桐乡乌镇共有n个人,请找出该镇上的前m个大富翁.
 

Input
输入包含多组测试用例.
每个用例首先包含2个整数n(0<n<=100000)和m(0<m<=10),其中: n为镇上的人数,m为需要找出的大富翁数, 接下来一行输入镇上n个人的财富值.
n和m同时为0时表示输入结束.
 

Output
请输出乌镇前m个大富翁的财产数,财产多的排前面,如果大富翁不足m个,则全部输出,每组输出占一行.
 

Sample Input
 
   
3 1 2 5 -1 5 3 1 2 3 4 5 0 0
 

Sample Output
 
   
5 5 4 3
 
用cin/cout超时,改成scanf/printf就ac了
#include<iostream>
#include<queue>
#include<cstdio> 
using namespace std;
int main()
{
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF&&(n||m))
	{
		priority_queue<int> s;
		int i,l,v;
		for(i=0;i<n;i++)
		{
			scanf("%d",&v);
			s.push(v);
		}
		if(m<=n)
		{
			for(i=0;i<m;i++)
			{
				printf("%d",s.top());
				s.pop();
				if(i!=(m-1)) printf(" ");
				else printf("\n");
			}
		}
		else
		{
			for(i=0;i<n;i++)
			{
				cout<<s.top();
				s.pop();
				if(i!=(n-1)) printf(" ");
				else printf("\n");
			}
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/luojiushenzi/article/details/80185326