UVA 540 Team Queue

Source: Internet
Author: User
Tags data structures ini

Original question:
Queues and priority Queues is data structures which is 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 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 and 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.
Input
The input file is 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 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 (both hundred thousand) commands, so the imple-
Mentation of the team queue should be Efficient:both enqueing and dequeuing of a 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 "the" case. Then,for each ' DEQUEUE ' command, print the element which is dequeued on a single line. Print a blank line after all 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

Effect:
Give a number n and tell you there are n organizations. And then I'll tell you the number of people in this n organization, and now these people are going to line up, and if the new person is on the team, you can insert it directly behind the person in your organization, and if you don't have an organization, insert it to the last side. Now let you manipulate the process.

#include <bits/stdc++.h> using namespace std;
Queue<int> que[1001];
int mark[1000001];
    void Ini () {memset (mark,0,sizeof (Mark));
    for (int. i=0;i<=1000;i++) {while (!que[i].empty ()) Que[i].pop ();
    }} int main () {Ios::sync_with_stdio (false);
    int t,n,m,k=1;
    String e= "ENQUEUE";
    String d= "DEQUEUE";
    String s= "STOP";
    string C;
        while (cin>>t,t) {ini ();
            for (int i=1;i<=t;i++) {cin>>n;
                for (int j=1;j<=n;j++) {cin>>m;
            Mark[m]=i;
        }} cout<< "Scenario #" <<k++<<endl;
            while (true) {cin>>c;
            if (c==s) break;
                    else {if (c==e) {cin>>m;
                    int i=mark[m];
                 if (Que[i].empty ())       Que[0].push (i);
                Que[i].push (m);
                    } else {Cout<<que[que[0].front ()].front () <<endl;
                    Que[que[0].front ()].pop ();
                if (Que[que[0].front ()].empty ()) Que[0].pop ();
    }}} cout<<endl;
} return 0;
 }

Answer:
First create a hash table that records the organization number for each player. Then set up two queues, one queue que[0] to record the queuing out of the organization number, and another queue que[i] to record the team in the organization. Now if there is a person belonging to the organization N, now this person in the team, if Que[n] is empty, that the person can only obediently stand to the end of the queue, and then in Que[0].push (n) otherwise directly in Que[n] to add this person's number. Out of the queue first look for que[0] head of the organization number n, if Que[n] out of the team after the empty, is the team of people without organization N, then Que[0].pop () otherwise que[n].pop () can

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.