一个FLAG #15# 团队队列

例题5-6 团队队列(Team Queue,UVA - 540)。完整题目见参考[1]

#include <cstdio>
#include <queue>
#include <map>
using namespace std;

const int MAXT = 1000 + 10;

int main()
{
    int t, kase = 0; // t是团队数量 
    while (scanf("%d", &t) == 1 && t) {
        printf("Scenario #%d
", ++kase);
        // 记录每个队员所对应的团队编号
        map<int, int> team; // 记录每个队员所在的团队 例如team[2] = 3 表示2号队员在3号团队里 
        for (int i = 0; i != t; ++i) {
            int n, x; // n 是i团队的队员总数 , x 是特定队员的编号 
            scanf("%d", &n);
            while (n--) {
                scanf("%d", &x);
                team[x] = i;
            }
        } 
        
        // 模拟
        queue<int> q, q2[MAXT]; // q 是团队的队列, 而q2[i]则是团队i成员的队列
        while (true) {
            int x;
            char cmd[10];
            scanf("%s", cmd);
            if (cmd[0] == 'S') {
                break;    
            } else if (cmd[0] == 'D') {
                int t = q.front();
                printf("%d
", q2[t].front());
                q2[t].pop(); 
                if (q2[t].empty()) {
                    q.pop(); // 如果该队所有队员都排完了,那么整个团队出队 
                }
            } else if (cmd[0] == 'E') {
                scanf("%d", &x);
                int t = team[x];
                if (q2[t].empty()) {
                    // 如果t队队员首次排队话 
                    q.push(t);
                }  
                q2[t].push(x); // 非首次直接push到“子队列”就可以了 
            }
        } 
        
        printf("
");
    }
    
    return 0;
}

参考

[1] https://vjudge.net/problem/UVA-540

原文地址:https://www.cnblogs.com/xkxf/p/12684545.html