"UVa 540" Team Queue

Source: Internet
Author: User

Team Queue

Queues and Priority Queues is data structures which is known to most computer scientists. The Team Queue, however, isn't so well known, though it occurs often in everyday life. At lunch time the "queue in front of the Mensa are 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) is 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 do like in normal queues:elements be processed from head to tail in the order they appear in the team que Ue.

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


The input file is contain one or more test cases. Each test case is begins with the number of teams T (). Then T-team descriptions follow, each one consisting of the number of elements belonging to the team and the elements them Selves. Elements is integers in the range 0-999999. A team may consist of up to elements.

Finally, a list of commands follows. There is 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 is terminated by a value of 0 for T.

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


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 was dequeued on a. Print a blank line after all test case, even after the last one.

Sample Input

101 102 1033 201 202 203ENQUEUE 101ENQUEUE 201ENQUEUE 102ENQUEUE 202ENQUEUE 103ENQUEUE 203DEQUEUEDEQUEUEDEQUEUEDEQUEUED EQUEUEDEQUEUESTOP25 259001 259002 259003 259004 2590056 260001 260002 260003 260004 260005 260006ENQUEUE 259001ENQUEUE 260 001ENQUEUE 259002ENQUEUE 259003ENQUEUE 259004ENQUEUE 259005DEQUEUEDEQUEUEENQUEUE 260002ENQUEUE 260003dequeuedequeuedequeuedequeuestop0

Sample Output

Scenario #1101102103201202203Scenario #2259001259002259003259004259005260001

The main problem is not to think too much. Open a map or an array of records to determine which person belongs to which team.

Then think of each team as a whole in a big queue. Each time out the team to judge the first person is in that team, is not the team's last.

The queue is similar.

#include <cstdio>#include<cstring>#include<map>#include<queue>using namespaceStd;map<int,int>belong;intNteam;intMain () {intTest =0;  while(SCANF ("%d", &nteam) &&Nteam) {printf ("Scenario #%d\n", ++Test); Queue<int>Bigteam; Queue<int> group[1005];  for(inti =0; i < Nteam; ++i) {intnm; scanf ("%d", &nm);  for(intj =0; J < nm; ++j) {intmember; scanf ("%d", &member); Belong[member]=i; }        }        Charcmd[Ten];  while(SCANF ("%s", CMD)) {            if(cmd[0] =='S') Break; if(cmd[0] =='E')            {                intx; scanf ("%d", &x); intTeamid =Belong[x]; if(Group[teamid].empty ()) Bigteam.push (Teamid);            Group[teamid].push (x); }            Else            {                intTeamid =Bigteam.front (); printf ("%d\n", Group[teamid].front ());                Group[teamid].pop (); if(Group[teamid].empty ()) Bigteam.pop (); }} printf ("\ n"); }    return 0;}

"UVa 540" Team Queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.