UVA-540 Team Queue (STL, 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 the 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 Teammat Es (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.

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 could contain up to 200000 (A. hundred thousand) commands, so the implementation of the team queue should be Efficient:both enqueing and dequeuing of a element should only take constant time.

OutputFor each test case, first print a line saying ' Scenario # k", where  k Is the number of the the test case. Then, for each DEQUEUE command, 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


Miguel A. Revilla
1999-01-11

T team of people in a long line, a new person, if his teammates in line, then the new people will jump to the end of the last teammate, or queue at the end of the long team.

There are three kinds of instructions:

ENQUEUE x: People numbered x Enter the long team.

DEQUEUE: Long team first out of line.

STOP: Stop the simulation.

Train of thought: use two queue to record long team arrangement situation.

#include <iostream> #include <cstdio> #include <queue> #include <string> #include <map> Using namespace Std;int main () {int Cas=0;int t;while (scanf ("%d", &t)! = eof&&t) {cout << "Scenario #" &L t;< ++cas << endl;int N, x;map<int, int>team;  Record the team number for everyone, team[x] represents the team number of the person with the number x. for (int i = 0; i < T; i++) {cin >> N;while (n--) {cin >> x;team[x] = i;}} String op;  int num;queue<int>teamate[1010];  Queues for members of individual teams queue<int>teamm;  Team queue for (;;) {cin >> op;if (op[0] = = ' S ') break;if (op[0] = = ' E ') {cin >> num;if (Teamate[team[num]].empty ()) Teamm.push (tea M[num]);  New Team Queue Teamate[team[num]].push (num);} if (op[0] = = ' D ') {int temp = Teamm.front (); cout << Teamate[temp].front () << Endl; Teamate[temp].pop (); if (team Ate[temp].empty ()) Teamm.pop ();  }}cout << Endl who have no team in the long Line;}}



UVA-540 Team Queue (STL, 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.