[UVA - 540] Team Queue (map, queue)

Team Queue

Descriptions:

Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example.

In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue.

Your task is to write a program that simulates such a team queue.

Input

The input file will contain one or more test cases. Each test case begins with the number of teams t (1 ≤ t ≤ 1000). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0..999999. A team may consist of up to 1000 elements.

Finally, a list of commands follows. There are three different kinds of commands:

• ENQUEUE x — enter element x into the team queue

• DEQUEUE — process the first element and remove it from the queue

• STOP — end of test case

The input will be terminated by a value of 0 for t

Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the implementation of the team queue should be efficient: both enqueing and dequeuing of an element should only take constant time.

Output

For each test case, first print a line saying ‘Scenario #k’, where k is the number of the test case. Then, for each ‘DEQUEUE’ command, print the element which is dequeued on a single line. Print a blank line after each test case, even after the last one.

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

Let me talk about the subject requirements

T has a team of people is a long row team. Each time a new person, if he had his teammates in the queue, then the newcomer will be the last to jump the queue to a teammate behind. If you do not line up any of his teammates, he will be discharged to the long lines of the tail. Enter all the players of each team in the numbers required to support the following three kinds of instruction (the first two instructions can be interspersed). 
ENQUEUE: X number of people into long lines. 
DEQUEUE: long lines the first team from the team. 
STOP: stop the simulation. 
For each DEQUEUE command, the output of a team of people numbering. 
Entry
Input file has one or more sets of test data. 
Each test began t teams. T below the line, the first number of each line represents the number of this team, these people are behind the numbers. Number is an integer between 0 to 999,999. 
Each test case to "STOP" end. 
Input is ended when t == 0. 
Warning: A test case may contain a maximum of 200,000 (two hundred thousand) command, so to achieve 
the team's queue should be effective. 
Export
For each test, prints a "Scenario #k", k is the first several sets of data. For each "DEQUEUE" command, the output of a number of people out of the team. After each set of test data to wrap, even the last set of test data.

Topic links:

https://vjudge.net/problem/UVA-540

Each person to find the corresponding team with map, then queue to queue numbers were stored number of teams and personnel

AC Code

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
#defineME0 (X) Memset (X, 0, the sizeof (X))
 the using  namespace STD;
 int main () 
{ 
    int n-;
     int SUM = 0 ;
     the while (CIN >> n-, n-) 
    { 
        SUM ++ ; 
        COUT << " # Scenario " << SUM << endl; 
        Map < int , int > team; // a person corresponding to a group number 
        for ( int I = 0 ; I <n-; I ++ ) 
        { 
            int X; 
            CIN>> X;
             for ( int J = 0 ; J <X; J ++ ) 
            { 
                int NUM; 
                CIN >> NUM; 
                Team [NUM] = I; // person ID number corresponding to his team 
            } 
        } 
        String S; 
        Queue < int > Big; // team queue, representatives of several teams 
        queue < int > Small [ 10005 ]; // for each queue of team members 
        the while (CIN = S >> S &&! " the STOP " ) 
        { 
            IF(S == " the ENQUEUE " ) 
            { 
                int NUM; 
                CIN >> NUM;
                 int T = Team [NUM]; // this person corresponding to the team 
                IF (Small [T] .empty ()) // if looking less than team to re-establish a team 
                    big.push (t); 
                Small [t] .push (NUM); // this man to make the team as 
            }
             IF (S == " DEQUEUE " ) 
            { 
                int t = Big .front (); // Take the first team 
                IF (! Small [T] .empty ()) // team is not empty, the person to pick up the team
                {
                    cout<<small[t].front()<<endl;
                    small[t].pop();
                }
                if(small[t].empty())
                    big.pop();
            }
        }
        cout<<endl;
    }
}

 

Guess you like

Origin www.cnblogs.com/sky-stars/p/10991934.html