题目大意:这道题是中文题,读者可直接去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;
else
return 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;
}