HDU 1387 Team Queue (priority queue)

Source: Internet
Author: User
Tags data structures printf
Team Queue Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1524 Accepted Submission (s): 515


Problem Description 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.


Input the input would contain one or more test cases. Each test case is 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 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.


Output for 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 was dequeued on a. 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 dequ Eue DEQUEUE DEQUEUE DEQUEUE STOP 2 5 259001 259002 259003 259004 259005 6 260001 260002 260003 260004 260005 260006 Enqueu E 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
Source University of Ulm Local Contest 1998
#include <stdio.h> #include <queue> #include <string.h> using namespace std;
const int N = 1000005;
    struct NODE {int pri1, pri2, id;
        friend bool operator< (node AA, node bb) {if (Aa.pri1 = = Bb.pri1) return aa.pri2>bb.pri2;
    return aa.pri1 > Bb.pri1;
}
};
int pri[1005], num[1005];
int team[n];
    void work () {priority_queue<node> q;
    NODE now;
    int ID, k = 0;
    Char str[100];
    memset (num, 0, sizeof (num));

    memset (PRI,-1, sizeof (PRI));
        while (scanf ("%s", str) > 0) {if (str[0] = = ' s ') break;
            if (str[0] = = ' E ') {scanf ("%d", &id);
            Now.id = ID;
            Now.pri2 = k;
            if (team[id] = =-1) now.pri1 = k;
                else if (pri[team[id]] = =-1) {now.pri1 = k;
                pri[Team[id]] = k;
            num[Team[id]]++;  } Else Now.pri1 =pri[Team[id]], num[team[id]]++;; Q.push (now);
        k++;
            } else{now = Q.top (); Q.pop ();
            num[Team[now.id]]--;

            if (num[team[now.id]]==0) pri[team[now.id]] =-1;
        printf ("%d\n", now.id);
}} printf ("\ n");

    } int main () {int T, n, id, _cas = 0;
        while (scanf ("%d", &t) >0 && t) {memset (Team,-1, sizeof (team));
            for (int i = 0; i < T; i++) {scanf ("%d", &n);
                while (n--) {scanf ("%d", &id);
            Team[id] = i;
        }} printf ("Scenario #%d\n", ++_cas);
    Work ();
 }
}

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.