码迷,mamicode.com
首页 > 其他好文 > 详细

优先队列题目集合

时间:2020-01-21 23:22:37      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:过程   pac   队列   原则   ios   输入数据   stream   cst   review   

       看病要排队这个是地球人都知道的常识。 不过经过细心的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
       7
       IN 1 1
       IN 1 2
       OUT 1
       OUT 2
       IN 2 1
       OUT 2
       OUT 1
       2
       IN 1 1
       OUT 1
       Sample Output
       2
       EMPTY
       3
       1
       1

 1 #include"iostream"
 2 #include"algorithm"
 3 #include"cstdio"
 4 #include"cstring"
 5 #include"string.h"
 6 #include"queue"
 7 using namespace std;
 8 struct node{
 9     int dj,id; 
10    bool operator <(const node &n)const{
11     if(dj!=n.dj) return dj<n.dj;
12     return id>n.id;
13  }
14 };
15 int main(){
16     int t,m,n;
17     while(~scanf("%d",&t)){      
18     priority_queue<node>q[4];
19       node temp;
20       int id=1;
21         while(t--){
22         string s;
23         cin>>s;
24         if(s=="IN"){
25         int a,b;
26             scanf("%d%d",&a,&b);
27             temp.dj=b;
28             temp.id=id;
29             id++;
30              q[a].push(temp);
31           }
32         else if(s=="OUT"){
33             int a;
34             scanf("%d",&a);
35             if(q[a].empty()){
36                 printf("EMPTY\n");
37             }
38         else{
39         printf("%d\n",q[a].top().id);
40              q[a].pop();
41           }
42         }
43         }
44     }
45     return 0;
46 }

 

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!

 1 #include"iostream"
 2 #include"algorithm"
 3 #include"cstdio"
 4 #include"cstring"
 5 #include"queue"
 6 using namespace std;
 7 struct node{    
 8     string s;    
 9     int m;
10     int dj;    
11     int id;    
12     };
13      node temp;
14     bool operator <(const node &a,const node &b){
15       if(a.dj==b.dj) return a.id>b.id;
16       return a.dj>b.dj;
17     }        
18 priority_queue<node>q;
19 int main(){
20         string ss;
21         int js=1;
22         while(cin>>ss){ 
23            if(ss=="GET"){
24                  if(q.empty()){
25                       cout<<"EMPTY QUEUE!"<<endl;
26                  }
27               else{
28                     cout<<q.top().s<<" "<<q.top().m<<endl;
29                     q.pop();
30               }
31            }
32            else{
33                  cin>>temp.s>>temp.m>>temp.dj;
34                  temp.id=js;
35                  js++;
36                  q.push(temp);
37            }    
38         }
39     return 0;    
40 }

 

优先队列题目集合

标签:过程   pac   队列   原则   ios   输入数据   stream   cst   review   

原文地址:https://www.cnblogs.com/huangdf/p/12227128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!