题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=1509
Windows Message Queue
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
GET
PUT msg1 10 5
PUT msg2 10 4
GET
GET
GET
Sample Output
EMPTY QUEUE!
msg2 10
msg1 10
EMPTY QUEUE!
优先队列裸题。。。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<string> 8 #include<queue> 9 #include<map> 10 using std::cin; 11 using std::cout; 12 using std::endl; 13 using std::find; 14 using std::sort; 15 using std::map; 16 using std::pair; 17 using std::vector; 18 using std::string; 19 using std::priority_queue; 20 #define pb(e) push_back(e) 21 #define sz(c) (int)(c).size() 22 #define mp(a, b) make_pair(a, b) 23 #define all(c) (c).begin(), (c).end() 24 #define iter(c) decltype((c).begin()) 25 #define cls(arr,val) memset(arr,val,sizeof(arr)) 26 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 27 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 28 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 29 const int N = 256; 30 typedef unsigned long long ull; 31 struct Node { 32 string name; 33 int val, fix, id; 34 Node() {} 35 Node(string nam, int v, int f, int i) : name(nam), val(v), fix(f), id(i){} 36 friend bool operator<(const Node &a, const Node &b) { 37 return a.fix == b.fix ? a.id > b.id : a.fix > b.fix; 38 } 39 }; 40 priority_queue<Node> que; 41 int main() { 42 #ifdef LOCAL 43 freopen("in.txt", "r", stdin); 44 freopen("out.txt", "w+", stdout); 45 #endif 46 int a, b, k = 1; 47 char buf[N], msg[N]; 48 while (gets(buf)) { 49 if (!strcmp(buf, "GET")) { 50 if (que.empty()) { puts("EMPTY QUEUE!"); continue; } 51 Node ret = que.top(); que.pop(); 52 printf("%s %d ", ret.name.c_str(), ret.val); 53 } else { 54 sscanf(buf + (strchr(buf, ' ') - buf) + 1, "%s %d %d", msg, &a, &b); 55 que.push(Node(msg, a, b, k++)); 56 } 57 } 58 return 0; 59 }