uva 540(Team Queue)

题目大意:

   有t个团队的人正在排一个长队。每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一个队友的身后。如果没有任何一个队友排队,则他会排到长队的队尾。

   输入每个团队中所有队员的编号,要求支持如下3种指令(前两种指令可以穿插进行)


对于每个DEQUEUE指令,输出出队人的编号。


题目分析:

   没有插队的时候,就是普通的队列问题,入队出队就可以了。有了插队应该怎么处理呢,把团队看成一个队列,如果有成员进来,加入到所属团队的队列。那么整个队列就变成了队列的队列;


参考代码:

#include <cstdio>
#include <cstring>
#include <map>
#include <queue>

using namespace std;

map<int, int> team; //team[x] 表示编号为x的人所在的团队;
queue<int> q, q2[1010];  //q队列中存放团队队列的编号; q2[i]表示第i个队伍的队列;
int inqueue[1010];

int main()
{
    int cas = 0;
    int t;  // t个团队;
    while(scanf("%d", &t) != EOF && t){
        int n, x;
        for(int i = 0; i < t; ++i){
            scanf("%d", &n);
            for(int j = 0; j < n; ++j){
                scanf("%d", &x);
                team[x] = i;
            }
        }
        char op[10];
        memset(inqueue, 0, sizeof(inqueue));
        printf("Scenario #%d\n", ++cas);
        while(scanf("%s", op) && op[0] != 'S'){
            if(op[0] == 'E'){
                scanf("%d", &x);
                if(!inqueue[team[x]]){   //判断这个人所在的团队有没有在队列中
                    q.push(team[x]);
                    inqueue[team[x]] = 1;
                }
                q2[team[x]].push(x);
            } else if(op[0] == 'D'){
                int outx;         // outx表示出队的人;
                int t = q.front(); // 获得队首的队列;
                while(q2[t].empty()){
                    q.pop();
                    inqueue[t] = 0;
                    t = q.front();
                }
                outx = q2[t].front();
                q2[t].pop();
                printf("%d\n", outx);
            }
        }
        printf("\n");
        while(!q.empty())
            q.pop();
        for(int i = 0; i < t; ++i)
            while(!q2[i].empty())
                q2[i].pop();
    }
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值