Windows Message Queue————栈和队列(优先队列)

﹏ヽ暗。殇╰゛Y 2022-05-18 02:59 306阅读 0赞

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<cstdio>
  2. #include<cstring>
  3. #include<string>
  4. #include<queue>
  5. #include<iostream>
  6. #include<vector>
  7. #include<stack>
  8. #include<algorithm>
  9. using namespace std;
  10. struct node{
  11. string name;
  12. int x,a,b;
  13. node(){}
  14. node(string _name,int _x,int _a,int _b)
  15. {
  16. name=_name;
  17. x=_x;
  18. a=_a;
  19. b=_b;
  20. }
  21. bool operator <(const node & n)const
  22. {
  23. return a==n.a?b>n.b:a>n.a;
  24. }
  25. };
  26. int main()
  27. {
  28. string s;
  29. priority_queue<node> q;
  30. ios::sync_with_stdio(false);
  31. int y=0;
  32. while(cin>>s)
  33. {
  34. if(s[0]=='G')
  35. {
  36. if(q.size())
  37. {
  38. node z=q.top();
  39. q.pop();
  40. cout<<z.name<<" "<<z.x<<endl;
  41. }
  42. else
  43. cout<<"EMPTY QUEUE!"<<endl;
  44. }
  45. else
  46. {
  47. y++;
  48. string na;
  49. int a,b;
  50. cin>>na>>a>>b;
  51. q.push(node(na,a,b,y));
  52. }
  53. }
  54. return 0;
  55. }

发表评论

表情:
评论列表 (有 0 条评论,306人围观)

还没有评论,来说两句吧...

相关阅读

    相关 优先队列(priority_queue)

    优先队列:优先级最高的先出队。 队列和排序的完美结合,可以存储数据,还可以把这些数据按设定的规则进行排序。 每次的push和pop操作,优先队列都会动态调整,把优先级最