UVA-540 队列中的队列 queue

51 篇文章 2 订阅

维护一个队列,队列的元素是一个队列。我们定义队列元素为短队,队列为长队。长队的元素是短队的索引,短队的元素是队员号。

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
const int MAXN = 1e3 + 10;
int main() {
    int n;
    int cnt = 0;
    while (~scanf("%d", &n) && n) {
        int m, x;
        map<int, int> team; // 记录队员到队伍的映射
        for (int i = 0; i < n; ++i) {
            scanf("%d", &m);
            for (int j = 0; j < m; ++j) {
                scanf("%d", &x);
                team[x] = i;
            }
        }
        // q1 长队,q2 所有短队
        queue<int> q1, q2[MAXN]; 
        char s[10];
        printf("Scenario #%d\n", ++cnt);
        while (1) {
            scanf("%s", s);
            if (s[0] == 'S') break;
            else if (s[0] == 'E') { // 入队
                scanf("%d", &x);
                int t = team[x]; // 拿到索引
                if (q2[t].empty()) { // 如果该队伍是空的,那么长队中没有该队伍,应该加入队伍索引
                    q1.push(t);
                }
                q2[t].push(x); // 短队元素入队
            } else if (s[0] == 'D') { // 出队
                int t = q1.front(); // 拿到长队队首索引
                printf("%d\n", q2[t].front());
                q2[t].pop(); // 短队元素出队
                if (q2[t].empty()) { // 如果短队为空,从长队中出队
                    q1.pop();
                }
            }
        }
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值