UVA 540 Team Queue

Source: Internet
Author: User

Team Queue
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &%llu

Submit Status

Description

Queues and Priority Queues is data structures which is known to most computer scientists. TheTeam 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 itsteammates (E Lements 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.

InputThe input file is contain one or more test cases. Each test case 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.

OutputFor each test case, first print a line saying 'Scenario # k", where kIs the number of the the test case. Then, for eachDEQUEUEcommand, print the element which is 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

Learning ~!

#include <iostream> #include <stdio.h> #include <queue> #include <map>using namespace Std;const    int Maxt=1023;int Main () {int t,n,x;    int x=0;        while (cin>>t,t) {cout<< "Scenario #" <<++X<<endl;        map< int, int >team;            for (int k=0; k<t; k++) {cin>>n;                for (int i=0; i<n; i++) {cin>>x;            Team[x]=k;        }} queue< int >q, Q2[MAXT];            while (1) {char cmd[21];            int x;            scanf ("%s", cmd);            if (cmd[0]== ' S ') break;                else if (cmd[0]== ' E ') {cin>>x;                int t=team[x];                if (Q2[t].empty ()) Q.push (t);            Q2[t].push (x);                } else if (cmd[0]== ' D ') {int t=q.front ();     printf ("%d\n", Q2[t].front ());           Q2[t].pop ();            if (Q2[t].empty ()) Q.pop ();    }} cout<<endl; } 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.