例题5-6 UVA 540 Team Queue团体队列

这个题又展示了queue:

整体思路:

输入的编号用map映射到团队编号中,在然后就是输入命令:

入队命令:

用 t 记录输入x的团队编号,在q2[t]找到x团队内,如果是空的话,就把整个团队加入到整个队列中,然后再把x加入到x团队中!

出队命令:

先用t记录队首的团队编号,在把队首的团队(q2[t])的队首去掉,如果该团队为空,则把整个团队去掉,依次类推。。

#include<cstdio>
#include<map>
#include<queue>
using namespace std;
int main()
{
    int T,cont = 0;
    const int maxt = 1000 + 10;
    while(~scanf("%d",&T) && T){
        printf("Scenario #%d\n",++cont);
        map<int,int>team;
        for (int i = 0; i  < T; ++i){
            int n,x;
            scanf("%d",&n);
            while(n--){
            scanf("%d",&x);
            team[x] = i;
        }
    }
        queue<int>q,q2[maxt];
        for(;;){
            char cmd[10];
            scanf("%s",cmd);
            if (cmd[0] == 'S')break;
            if (cmd[0] == 'E'){
                int x;
                scanf("%d",&x);
                int t = team[x];
                if (q2[t].empty())q.push(t);
                q2[t].push(x);

            }
            if (cmd[0] == 'D'){
                int t = q.front();
                printf("%d\n",q2[t].front());
                q2[t].pop();
                if (q2[t].empty())q.pop();
            }

        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值