设为首页 加入收藏

TOP

团体队列 UVA540 Team Queue
2019-03-02 12:07:56 】 浏览:64
Tags:团体 队列 UVA540 Team Queue

题目描述

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

输入每个团队中所有队员的编号,要求支持如下3中指令:

ENQUEUE x:编号为x的人进入长队

DEQUEUE:长队的队首出队

STOP:停止模拟

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

样例输入

2
3 101 102 103
3 201 202 203
ENQUEUE 101
ENQUEUE 201
ENQUEUE
102
ENQUEUE 202
ENQUEUE 103
ENQUEUE
203
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
2
5
259001 259002 259003 259004 259005
6 260001 260002 260003 260004 260005
260006
ENQUEUE 259001
ENQUEUE 260001
ENQUEUE 259002
ENQUEUE
259003
ENQUEUE 259004
ENQUEUE 259005
DEQUEUE
DEQUEUE
ENQUEUE
260002
ENQUEUE
260003
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0

样例输出

Scenario #1
101
102
103
201
202
203
Scenario #2
259001
259002
259003
259004
259005
260001

#define LOCAL
#include<iostream>
#include<cstdio>
#include<map>
#include<queue>
#include<string>

using namespace std;

const int maxn=100;

int main(){
    #ifdef LOCAL
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    #endif

    //!1.记录所有人的团队编号,从0开始
    int t;//共有t个团队
    int kase=0;
    while(scanf("%d",&t) == 1 && t>0){
        map<int,int> team;
        cout<<"Scenario #"<<++kase<<endl;
        for(int i=0;i<t;i++){//第i个团队
            //有n个人
            int n;
            cin>>n;
            int code;
            while(n--){scanf("%d",&code);team[code]=i;}
        }

        //!2.模拟
        queue<int> q,q2[maxn];
        while(1){
            int x;
            string cmd;
            cin>>cmd;
            if(cmd[0] == 'S')break;
            else if(cmd[0] == 'D'){
                x=q.front();
                cout<<q2[x].front()<<endl;
                q2[x].pop();
                if(q2[x].empty())q.pop();
            }
            else if(cmd[0] == 'E'){
                cin>>x;
                int t=team[x];
                if(q2[t].empty())q.push(t);
                q2[t].push(x);
            }
        }
        cout<<endl;
    }
    return 0;
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇丑数(Ugly Numbers, UVa 136) 下一篇洛谷P4588 [TJOI2018]数学计算(线..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目