uva 540 Team Queue

來源:互聯網
上載者:User

原題:
Queues and Priority Queues are data structures which are 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 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 its teammates (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 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 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:
• 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 will be terminated by a value of 0 for t.
Warning: A test case may contain up to 200000 (two hundred thousand) commands, so the imple-
mentation of the team queue should be efficient: both enqueing and dequeuing of an 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 test case. Then,for each ‘DEQUEUE’ command, 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
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

大意:
給一個數n,告訴你有n個組織。然後給你告訴你這個n個組織裡面人員的編號,現在這些人要排隊,如果新來一個人入隊的話可以直接插入到自己組織的人的後面,如果沒有組織就插入到最後面。現在讓你操作這個過程。

#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;}

解答:
首先建立一個hash表,用來記錄每個隊員所對應的組織編號。然後建立兩個隊列,一個隊列que[0]記錄組織編號的入隊出對情況,另外一個隊列que[i]用來記錄組織裡的人出隊入隊的情況。現在假如有一個人屬於組織n,現在這個人入隊,如果que[n]是空的,說明這個人只能乖乖的站到隊列尾部,然後在que[0].push(n)否則直接在que[n]當中加入這個人的編號。出隊列的時候先尋找que[0]頭部的的組織編號n,如果que[n]出隊後空了,就是隊伍中沒有組織n的人,那麼que[0].pop()否則que[n].pop()即可

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.