UVa - 540 Team Queue

题意

有一个长队包含几个小队, 每个小队连在一起排成一个长队, 有以下三种操作 :

① ENQUEUE 在队列中输入元素x
② DEQUEUE 输出长队队首并将其从队列中移除
③ STOP 结束处理

如果有队友就站到自己所属队列的最末, 如果没有队友就站到整个长队的最末

AC代码

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

const int maxn = 1000 + 10;
int main()
{
    int q,x;
    int casenum = 0;
    while( ~scanf("%d",&q) && q )
    {
        printf("Scenario #%d\n", ++casenum);
        map<int, int> team;
        for( int i = 1; i <= q; i++ )
        {
            int n;
            scanf("%d",&n);
            while( n-- ){
                scanf("%d",&x);
                team[x] = i;  //记录编号为x的人所在的队伍为第i队
                //printf("%d,",team[x]);
            }
        }
        queue<int> q, sq[maxn];
        //q是长队的队列,sq[i]是小队i成员的队列
        for(;;)
        {
            char s[maxn];
            scanf("%s",s);
            int mrk = s[0];
            if( mrk == 'S' )
            {
                puts("");  //Print a blank line
after each test case, even after the last one.
                break;
            }
            else if( mrk == 'E' )
            {
                scanf("%d", &x);
                int t = team[x];
                if(sq[t].empty())
                    q.push(t); //团队t进入队列
                sq[t].push(x);
            }
            else if( mrk == 'D' )
            {
                int t = q.front();
                printf("%d\n", sq[t].front());
                sq[t].pop();
                if(sq[t].empty())
                    q.pop(); //团体t全体出队列
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值