幸福列车Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 3050 Accepted Submission(s): 993 Problem Description 一批幸福的列车即将从杭州驶向幸福的终点站——温州,身为总列车长的linle有一些奇怪的癖好。
Input 本题包含多组测试,请处理到文件结束。
Output 对于每个 GETOUT 命令,输出被踢出的那个人的名字
Sample Input
Sample Output xhd zl hoho Hint Huge input, scanf is recommended.
Author linle
Source
Recommend lcy |
/* HDU1434 幸福列车 */
#include <iostream>
#include <queue>
using namespace std;
const int N = 10000;
struct _node {
string name;
int rp;
friend bool operator < (_node a, _node b) {//运算符重载
if(a.rp != b.rp)
return a.rp > b.rp;
else
return a.name < b.name;
}//排序,将队列中的人按照题意进行排序,以便后续直接踢出
};
int main()
{
std::ios::sync_with_stdio(false);//加快c++的输入输出速度的代码
int n, m, xi, xj;
while(cin >> n >> m) {
priority_queue<_node> q[N+1];
_node t; // 临时的
for(int i=1; i<=n; i++) {
cin >> xi;
while(xi--) {
cin >> t.name >> t.rp;//读入人名和人品
q[i].push(t);//push到第i个队列
}//31__38都是初始化代码
}
while(m--) {//模拟m个指令,做m个循环
string op;
cin >> op;//读入命令
if(op == "GETON") {//根据不同的命令做判断,做不同的处理,geton是上车
cin >> xi >> t.name >> t.rp;
q[xi].push(t);
} else if(op == "JOIN") {//把一个车厢里面的人导入另一个车厢
cin >> xi >> xj;
while(!q[xj].empty()) {
t = q[xj].top();//先暂存在t中,在导入xi中,也可以直接导,不用存在t中了,比较繁琐
q[xi].push(t);
q[xj].pop();//xj中的释放
}
} else if(op == "GETOUT") {
cin >> xi;
t = q[xi].top();//找在前面的人,踢出
cout << t.name << endl;
q[xi].pop();
}
}
}
return 0;
}
/*
输入:
3 5 三辆列车,执行5次操作
2 有两个人在一车厢
xhd 0 名字xhd 人品0
zl 1 名字zl人品1
2 有两个人在二车厢
8600 1 名字8600 人品1
ll 2 名字ll 人品2
1 有一个人在三车厢
Ignatius 3 名字 Ignatius,人品2
GETOUT 1 一车厢踢出一个人
JOIN 1 2 合并一、二车厢
GETOUT 1 一车厢踢出一个人
GETON 3 hoho 2 三车厢上了一个名字hoho,人品为2的人
GETOUT 3 三车厢踢出一个人
输出:
xhd
zl
hoho
分析:
有几个车厢,就得有几个队列,而且得是优先队列。
合理的数据结构表示是关键
*/