暑期集训1:C++&STL 练习题D:HDU-1509

2018学校暑期集训第一天——C++与STL

练习题D ——  HDU - 1509 

D - 振电迁移的日蚀

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!

emmm……这道题和E题当时没有时间来不及写了……

问题描述
消息队列是windows系统的基本(basic fundamental)。对于每个程序,系统都会有一个消息队列。如果这个程序被操作,比如鼠标点击,文本(text)修改,系统将会给队列发送消息。与此同时,在非空的情况下,程序将会根据优先级值(priority value)从队列中不断得到消息。注意,优先级值越低表示的优先级越高。在这个问题中,你要模仿接收消息和发送消息的队列。

输入
输入项中只有一种情况。每一行是一个指令,接受或发送,即接收消息和发送消息。如果指令是发送,则会有一个表示消息名称的字符串还有两个整数,一个表示参数(parameter),一个表示优先权(priority)最多会有60000条指令。注意,一条消息能够出现两次或者更多,如果两条消息具有同样的优先权,那么先接收的那条将被执行(IE,FIFO都是这样的)。直至文件结束。

输出
对于每条接受指令,从消息队列中输出一行消息名和参数。如果在队列中没有消息,那么输出空队列。对于接受指令则没有输出。

#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

struct Message {
    static int idFactory;

    string name;
    int id, parm, priority;

    static int getID() {
        return idFactory++;
    }

    Message(string name = "", int parm = 0, int priority = 0) {
        this->id = getID();
        this->name = name;
        this->parm = parm;
        this->priority = priority;
    }

    bool operator < (const Message &rhs) const {
        if (priority == rhs.priority) {
            return id > rhs.id;
        } else {
            return priority > rhs.priority;
        }
    }

    void print() const {
        cout << name << " " << parm << endl;
    }
};

int Message::idFactory = 0;

int main(int argc, char *argv[]) {
    priority_queue<Message> pq;
    string command, name;
    int parm, priority;

    ios::sync_with_stdio(false);
    while (cin >> command) {
        if (command == "GET") {
            if (pq.empty()) {
                cout << "EMPTY QUEUE!" << endl;
            } else {
                pq.top().print();
                pq.pop();
            }
        } else {
            cin >> name >> parm >> priority;
            Message newMsg(name, parm, priority);
            pq.push(newMsg);
        }
    }

    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值