題目大意:這道題是中文題,讀者可直接去OJ上看題目
解題思路:題意並不難理解。在我們的現實生活中,假如我們要找某一個醫生看病,是不是就要到他的那一條隊列上去排隊???
而這個隊列又能根據多種情況來排序,這時候,我們可以考慮用以下優先隊列。。。。
代碼如下:
版本一(400MS左右):
/* * 1873_2.cpp * * Created on: 2013年8月8日 * Author: Administrator */#include <iostream>#include <queue>using namespace std;struct Node {int pri;int num;public:friend bool operator<(const Node& a, const Node& b) {if (a.pri != b.pri) {return a.pri < b.pri;}return a.num > b.num;}};int main() {int n;while (cin >> n) {priority_queue<Node> q[4]; int b,count = 1; string str; Node a;for (int i = 0; i < n; ++i) {cin >> str;if (str == "IN") {cin >> b >> a.pri;a.num = count++;q[b].push(a);} else {cin >> b;if (!q[b].empty()) {a = q[b].top();q[b].pop();cout<<a.num<<endl;}else{cout<<"EMPTY"<<endl;}}}}}
版本二:
/* * 1873_1.cpp * * Created on: 2013年8月8日 * Author: Administrator */#include"stdio.h"#include"string.h"#include"stdlib.h"#include"queue"using namespace std;int tot;struct node {int pri;int num;friend bool operator<(node n1, node n2) {if (n1.pri == n2.pri)return n2.num < n1.num;elsereturn n1.pri < n2.pri;}};int main() {int n;node now;int a, b;char str[20];while (scanf("%d", &n) != -1) {priority_queue<node> q[4];tot = 0;while (n--) {scanf("%s", str);if (strcmp(str, "OUT") == 0) {scanf("%d", &a);if (q[a].empty())printf("EMPTY\n");else {now = q[a].top();q[a].pop();printf("%d\n", now.num);}} else if (strcmp(str, "IN") == 0) {scanf("%d%d", &a, &b);now.num = ++tot;now.pri = b;q[a].push(now);}}}return 0;}