zoukankan      html  css  js  c++  java
  • hdu 1434 幸福列车

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=1434

    幸福列车

    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

    优先队列吧,队列合并的时候用可并堆要快一些吧,哎。懒得写了。。

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<vector>
     7 #include<string>
     8 #include<queue>
     9 #include<map>
    10 using std::cin;
    11 using std::cout;
    12 using std::endl;
    13 using std::find;
    14 using std::sort;
    15 using std::map;
    16 using std::pair;
    17 using std::string;
    18 using std::vector;
    19 using std::multimap;
    20 using std::priority_queue;
    21 #define pb(e) push_back(e)
    22 #define sz(c) (int)(c).size()
    23 #define mp(a, b) make_pair(a, b)
    24 #define all(c) (c).begin(), (c).end()
    25 #define iter(c) decltype((c).begin())
    26 #define cls(arr,val) memset(arr,val,sizeof(arr))
    27 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    28 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
    29 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
    30 const int N = 10010;
    31 typedef unsigned long long ull;
    32 struct Node {
    33     int rp;
    34     string name;
    35     Node(int i = 0, string j = "") :rp(i), name(j) {}
    36     inline bool operator<(const Node &a) const {
    37         return rp == a.rp ? name < a.name : rp > a.rp;
    38     }
    39 };
    40 priority_queue<Node> que[N];
    41 int main() {
    42 #ifdef LOCAL
    43     freopen("in.txt", "r", stdin);
    44     freopen("out.txt", "w+", stdout);
    45 #endif
    46     char buf[22],tmp[22];
    47     int n, m, rp, xi, xj;
    48     while (~scanf("%d %d", &n, &m)) {
    49         for (int i = 1; i <= n; i++) {
    50             scanf("%d", &xi);
    51             while (xi--) {
    52                 scanf("%s %d", buf, &rp);
    53                 que[i].push(Node(rp, buf));
    54             }
    55         }
    56         while (m--) {
    57             scanf("%s", buf);
    58             if (!strcmp(buf, "GETON")) {
    59                 scanf("%d %s %d", &xi, tmp, &rp);
    60                 que[xi].push(Node(rp, tmp));
    61             } else if (!strcmp(buf, "JOIN")) {
    62                 scanf("%d %d", &xi, &xj);
    63                 while (!que[xj].empty()) {
    64                     que[xi].push(que[xj].top());
    65                     que[xj].pop();
    66                 }
    67             } else {
    68                 scanf("%d", &xi);
    69                 printf("%s
    ", que[xi].top().name.c_str());
    70                 que[xi].pop();
    71             }
    72         }
    73         rep(i, n + 1) while (!que[i].empty()) que[i].pop();
    74     }
    75     return 0;
    76 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    PAT 1010. 一元多项式求导 (25)
    PAT 1009. 说反话 (20) JAVA
    PAT 1009. 说反话 (20)
    PAT 1007. 素数对猜想 (20)
    POJ 2752 Seek the Name, Seek the Fame KMP
    POJ 2406 Power Strings KMP
    ZOJ3811 Untrusted Patrol
    Codeforces Round #265 (Div. 2) 题解
    Topcoder SRM632 DIV2 解题报告
    Topcoder SRM631 DIV2 解题报告
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4632419.html
Copyright © 2011-2022 走看看