Ultraviolet A 540-team queue

Source: Internet
Author: User
Ultraviolet A 540-team queuetable OF CONTENTS
  • 1. Question
  • 2 ideas
  • 3 code
  • 4. Reference
1. Question

==============================


Team queue


QueuesAndPriority QueuesAre data structures which are known to most computer scientists.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 itsTeammates(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 in 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 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:

  • EnqueueX-Enter ElementXInto 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 in up to 200000 (two hundred thousand) commands, so the implementation of the team queue shocould be efficient: Both enqueing and dequeuing of an element shocould 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 eachDequeueCommand,
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

23 101 102 1033 201 202 203ENQUEUE 101ENQUEUE 201ENQUEUE 102ENQUEUE 202ENQUEUE 103ENQUEUE 203DEQUEUEDEQUEUEDEQUEUEDEQUEUEDEQUEUEDEQUEUESTOP25 259001 259002 259003 259004 2590056 260001 260002 260003 260004 260005 260006ENQUEUE 259001ENQUEUE 260001ENQUEUE 259002ENQUEUE 259003ENQUEUE 259004ENQUEUE 259005DEQUEUEDEQUEUEENQUEUE 260002ENQUEUE 260003DEQUEUEDEQUEUEDEQUEUEDEQUEUESTOP0

Sample output

Scenario #1101102103201202203Scenario #2259001259002259003259004259005260001

Miguel A. Revilla
2017-01-11

==============================

2 ideas

This is a dual queue. A queue is formed between the queue and the queue. The queue is also a queue. There is no difficulty, but it takes time into consideration. Queue operations should be performed using arrays. At the same time, the position of each queue in the queue should be recorded, and the number of the queue where each number is located should be changed to space for time.

3 code
/* * Problem: UVa 540 Team Queue * Lang: ANSI C * Time: 0.042s * Author: minix */#include <stdio.h>#define T 1000+10#define N 1000000+10int g[N];int l[T];int q[T], qh, qt;int t[T][T],th[T], tt[T];int main() {  int n, m, i, j, k, e, cg;  char line[50], cmd[10];  k = 1;  while (scanf("%d",&n)!=EOF) {    if (n == 0) break;    for (i=0; i<n; i++) {      scanf("%d",&m);      l[i] = -1; th[i] = tt[i] = 0;      for (j=0; j<m; j++) {        scanf ("%d",&e);        g[e] = i;      }    }    qh = qt = 0;    printf ("Scenario #%d\n", k++);    while (fgets(line,sizeof(line[0])*50,stdin)) {      if (line[0]=='S') break;      if (line[0]=='E') {        sscanf (line, "%s %d\n", cmd, &e);        cg = g[e];        if (l[cg] == -1) {          l[cg] = qt;          q[qt++] = cg;        }        t[cg][tt[cg]++] = e;      } else if (line[0]=='D') {        cg = q[qh];        printf ("%d\n", t[cg][th[cg]++]);        if (th[cg] == tt[cg]) {          qh++;          l[cg] = -1;        }      }    }    printf ("\n");  }  return 0;}
4 reference 1. http://uva.onlinejudge.org/external/5/540.html
Related Article

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.