Group queue UVA540 Team Queue

Keywords: C++

Title Description

There are t teams in a long line. Every time a new player comes in, if he has teammates in line, the new player will jump behind the last teammate. If there is no team mate in line, he will be placed at the end of the long line.

Enter the number of all team members in each team, and support the following 3 instructions:

ENQUEUE x: people with number x enter the long team

DEQUEUE: the first team of a long team

STOP: STOP simulation

For each DEQUEUE command, output the number of the person leaving the queue

sample input

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

sample output

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. Record everyone's team number, starting at 0
    int t;//t teams in total
    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++){//Team i
            //There are n individuals.
            int n;
            cin>>n;
            int code;
            while(n--){scanf("%d",&code);team[code]=i;}
        }

        //2. simulation
        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;
}

Posted by balloontrader on Sun, 08 Dec 2019 09:47:24 -0800