题目链接:hdoj 1434 幸福列车
幸福列车
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 2134 Accepted Submission(s): 662
Problem Description
一批幸福的列车即将从杭州驶向幸福的终点站——温州,身为总列车长的linle有一些奇怪的癖好。
他会记录下全部乘客的名字(name)和他们的人品值(RP),根据这些将他们排序,并不时地从某辆列车里踢出人品最不好(RP值最低)的一个人,当两个人人品一样不好时,他就会踢出名字难听的人(linle认为按字典顺序,排在越在后面的人名字越难听)。
当然出于列车行驶需要,他还会不时的发布一些命令,比如让某个乘客上车,合并某两辆列车等。
linle的上一任秘书*因为不能高效地执行他的这些命令而被炒鱿鱼,他现在正在寻觅新的秘书人选,你能不能胜任呢?(谢绝男士,待遇丰厚~~~)
Input
本题包含多组测试,请处理到文件结束。
对于每一组测试,第一行包含两个整数 N ,M ,表示一共有N( N<=10000 ) 辆列车,执行M( M<=10000 )次操作。
接下来有 N (从1开始记数)辆列车的信息,每辆列车先有一个数字 Xi(1 <= Xi <= 100 ),表示该列车有Xi个乘客,接下来Xi行乘客信息,每个乘客包含名字(20个字符以内,不包含空白符)和人品(0<= RP <=30000)。
再接下来有 M 行操作信息,一共有3种操作,分别为
GETON Xi name RP 表示有一个叫name的人品为RP的人登上第Xi列车
JOIN Xi Xj 表示有将第Xj辆列车合并到Xi辆列车
GETOUT Xi 表示从第Xi辆列车踢出一个人品最差的人
测试数据保证每个操作均合法,即不会将已经被合并到其他列车的列车再进行合并,也不会从一辆空列车里踢出乘客
Output
对于每个 GETOUT 命令,输出被踢出的那个人的名字
Sample Input
3 5
2
xhd 0
zl 1
2
8600 1
ll 2
1
Ignatius 3
GETOUT 1
JOIN 1 2
GETOUT 1
GETON 3 hoho 2
GETOUT 3
Sample Output
xhd
zl
hoho
Hint
Huge input, scanf is recommended.
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e5 + 10;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
struct Node {
int rp;
char name[25];
friend bool operator < (Node a, Node b) {
if(a.rp != b.rp) return a.rp > b.rp;
return strcmp(a.name, b.name) < 0;
}
};
int main() {
int n, m;
while(scanf("%d%d", &n, &m) != EOF) {
priority_queue<Node> Q[10001];
for(int i = 1; i <= n; i++) {
int num; scanf("%d", &num);
while(num--) {
char str[30]; int a;
scanf("%s%d", str, &a);
Node now;
now.rp = a; strcpy(now.name, str);
Q[i].push(now);
}
}
while(m--) {
char op[20]; int x, y, z;
char str[30]; scanf("%s", op);
if(strcmp(op, "GETOUT") == 0) {
scanf("%d", &x);
Node a = Q[x].top();
printf("%s\n", a.name);
Q[x].pop();
}
else if(strcmp(op, "JOIN") == 0) {
scanf("%d%d", &x, &y);
while(!Q[y].empty()) {
Node a = Q[y].top();
Q[x].push(a);
Q[y].pop();
}
}
else {
scanf("%d%s%d", &x, str, &y);
Node now; now.rp = y;
strcpy(now.name, str);
Q[x].push(now);
}
}
}
return 0;
}