priority queue

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 by;
	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;
}
intmain()
{
	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=my.top();
				cout<<cur.name<<" "<<cur.par<<endl;
				month.pop();
			}
		 }
		else
		{
			next.num=k++;
			cin>>next.name>>next.par>>next.pri;
			mes.push(next);
		}
	}
	return 0;
}

Queue to see a doctor

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


Problem Description
It is common knowledge that people on earth have to wait in line to see a doctor.
However, after careful observation of 0068, he found that the queue in the hospital is still very particular. There are three doctors (Khan, so few) in the hospital that 0068 went to see a doctor at the same time. The patients who see a doctor have serious illnesses, so they cannot be based on a simple first-come, first-served principle. So hospitals set 10 different priorities for each condition. Level 10 has the highest priority and level 1 has the lowest priority. When a doctor sees a doctor, he will select a person with the highest priority in his team for diagnosis and treatment. If two patients with the same priority are encountered, the first patient to queue is selected.

Now please help the hospital to simulate the process of seeing a doctor.
 

Input
The input data contains multiple sets of tests, please process until the end of the file.
The first row of each set of data has a positive integer N (0<N<2000) to indicate the number of events that occurred.
Next there are N lines representing the events that occurred.
There are two kinds of events:
1: "IN A B", which means that a patient with priority B requests doctor A for diagnosis and treatment. (0<A<=3, 0<B<=10)
2: "OUT A", it means that Doctor A conducted a diagnosis and treatment, and the patient was discharged after the diagnosis and treatment. (0<A<=3)
 

Output
For each "OUT A" event, please output the patient's ID number on one line. "EMPTY" is output if no patient required medical attention at the time of the event.
The definition of the ID of the patient is: in a set of tests, when the "IN A B" event occurs the Kth time, the ID of the incoming patient is K. Numbering starts from 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;
}
intmain()
{
	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
Please output the number of properties of the top m monopoly in Wuzhen, and the ones with more properties are listed first. If there are less than m monopoly, output all of them, and each group of output occupies one line.
 

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

Sample Output
 
   
5 5 4 3
 
Use cin/cout to time out, change to scanf/printf and ac
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
intmain()
{
	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;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325942613&siteId=291194637