幸福列车(优先队列模拟)

幸福列车

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 2169    Accepted Submission(s): 672

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.

题解:优先队列;按照题意水一水就好了;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN = 10010;
struct Node{
    char nm[21];
    int rp;
    friend bool operator < (Node a,Node b){
        if(a.rp != b.rp)return a.rp > b.rp;
        else {
            if(strcmp(a.nm,b.nm) < 0)return 1;
            else return 0;
        }
    }
};
priority_queue<Node>q[MAXN];
int main(){
    int N,M,x;
    while(~scanf("%d%d",&N,&M)){
        Node a;
        for(int i = 1;i <= N;i++){
            while(!q[i].empty())q[i].pop();
            scanf("%d",&x);
            while(x--){
                scanf("%s%d",a.nm,&a.rp);
                q[i].push(a);
            //    printf("***%s\n",q[i].top());
            }
        }
    //    printf("***%s\n",q[1].top());
        char s[10];
        int xi,xj;
        while(M--){
            scanf("%s",s);
            if(strcmp(s,"GETON") == 0){
                scanf("%d%s%d",&xi,a.nm,&a.rp);
                q[xi].push(a);
            }
            else if(strcmp(s,"JOIN") == 0){
                scanf("%d%d",&xi,&xj);
                while(!q[xj].empty()){
                    q[xi].push(q[xj].top());
                    q[xj].pop();
                }
            }
            else if(strcmp(s,"GETOUT") == 0){
                scanf("%d",&xi);
                printf("%s\n",q[xi].top().nm);
                q[xi].pop();
            }
        }
    }
    return 0;
}
时间: 2024-08-19 00:28:20

幸福列车(优先队列模拟)的相关文章

【优先队列】hdu 1434 幸福列车

用优先队列模拟: 1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 const int MAXN = 10000+10; 7 using namespace std; 8 9 typedef struct{ 10 string name; 11 int rp; 12 }node; 13 14

HDU5437 Alisha’s Party(优先队列+模拟)

Alisha's Party Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 4075    Accepted Submission(s): 1052 Problem Description Princess Alisha invites her friends to come to her birthday party. Each

HDU3810 Magina(搜索+用优先队列模拟01背包)经典

Magina Time Limit: 60000/30000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 528    Accepted Submission(s): 177 Problem Description Magina, also known as Anti-Mage, is a very cool hero in DotA (Defense of the Anci

优先队列+模拟-Fox and Number Game

codeforces-Fox and Number Game 题目地址:http://codeforces.com/contest/389/problem/A Time Limit: 1000ms Fox Ciel is playing a game with numbers now. Ciel has n positive integers: x1, x2, ..., xn. She can do the following operation as many times as needed:

hdu杭电1434 幸福列车【优先队列】

Problem Description 一批幸福的列车即将从杭州驶向幸福的终点站--温州,身为总列车长的linle有一些奇怪的癖好. 他会记录下全部乘客的名字(name)和他们的人品值(RP),根据这些将他们排序,并不时地从某辆列车里踢出人品最不好(RP值最低)的一个人,当两个人人品一样不好时,他就会踢出名字难听的人(linle认为按字典顺序,排在越在后面的人名字越难听). 当然出于列车行驶需要,他还会不时的发布一些命令,比如让某个乘客上车,合并某两辆列车等. linle的上一任秘书***因为不

HDU 1873-看病要排队(优先队列+模拟乱搞)

看病要排队 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5042    Accepted Submission(s): 2073 Problem Description 看病要排队这个是地球人都知道的常识. 不过经过细心的0068的观察,他发现了医院里排队还是有讲究的.0068所去的医院有三个医生(汗,这么少)同时看病.而看病的人病

HDU 1434 幸福列车

优先队列的应用 #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define maxn 10010 struct Node { string name; int rp; friend bool operator <(Node a,Node b) { if(a.rp != b.rp) return a.rp > b

Problem E: 穷游中国在统题 优先队列 + 模拟

http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=4 Problem E: 穷游中国在统题 Description Travel_poorly队是广工大目前最年轻的金牌队伍,队内成员分别是tmk.YFQ.Maple.这天他们接到教练的order要给新生杯统题,统题是个不轻松的工作,要评估各个题目的难度,设计出一套有梯度的套题,使得做题的情况有区分度.tmk很快想出了解决的办法,他给每道题目都设置了一个难度值,然后按照难度值进行筛选题

hdu 5441 优先队列+模拟 **

比赛的时候虽然考虑到没门的情况,但是写了几组都能过,就没想了,23333,差一行代码就能A,遗憾~~ 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #de