1. 程式人生 > >POJ 2259-Team Queue[佇列]

POJ 2259-Team Queue[佇列]

Description

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 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 implementation 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
Source

Ulm Local 1998
.
.
.
.
.

程式:
#include <iostream>
#include <queue>
using namespace std;
int team[999999];
int main()
{
    int t,k=1,x;
    while (cin>>t)
    {
        if (t==0) break;
        cout<<"Scenario #"<<k<<endl;
        k++;
        for (int i=0;i<t;i++)
        {
            int n;
            cin>>n;
            for (int j=0;j<n;j++)
            {
                cin>>x;
                team[x]=i;
            }
        }
        queue <int> q,q2[1001];
        while (1!=0)
        {
            int x;
            char ch[8];
            cin>>ch;
            if (ch[0]=='S') break; else 
            if (ch[0]=='E')
            {
                cin>>x;
                int t=team[x];
                if (q2[t].empty()) q.push(t);
                q2[t].push(x);
            } else 
            if (ch[0]=='D')
            {
                int t=q.front();
                cout<<q2[t].front()<<endl;
                q2[t].pop();
                if (q2[t].empty()) q.pop(); 
            }
        }
        cout<<endl;
    }
    return 0;
}

相關推薦

POJ 2259-Team Queue[佇列]

Description Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, i

POJ 2259 Team Queue(STL隊列)

實現 結構 pop 裏的 出隊 序號 入隊 pac 數量 轉自:https://www.cnblogs.com/yjlblog/p/7056746.html 題目背景 隊列和優先級隊列是大多數計算機科學家都知道的數據結構。但是團隊隊列卻不被人熟知,盡管在生活中經常出現。比如

[POJ2259]Team Queue (佇列,模擬)

2559是棧,2259是佇列,真的是巧啊 題意 模擬佇列 思路 水題 程式碼 因為太水,不想打,發部落格只是為了與2559照應,於是附上lyd的std   #include <queue> #include <cstdio> #include &

POJ 2259Team Queue佇列

題目大意: 有tt個團隊的人正在排一個長隊。每次新來一個人時,如果他有隊友在排隊,那麼新人會插隊到最後一個隊友的身後。如果沒有任何一個隊友排隊,則他會被排到長隊的隊尾。 要求支援如下3中指令:

Team Queue POJ - 2259 (隊列)

finally ast 題意 sed sam output nts app ever Queues and Priority Queues are data structures which are known to most computer scientists. Th

Team Queue佇列陣列與map搜尋)

1.題面: 題意 有t個團隊的人正在排一個長隊。每次新來一個人時,如果他有隊友在排隊,那麼這個新人會插隊到最後一個隊友身後。如果沒有任何一個隊友排隊,則他會排到長隊的隊尾。輸入每個團隊中所有隊員的編號,要求支援如下3種指令(前兩種指令可以穿插進行)。 ENQUEUE:編號為X的人進入長隊。

poj 3125 Printer Queue佇列

The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer que

經典第五章 例 5-6 UVA 540 Team Queue佇列的簡單應用)【queue

【題目分析】用佇列來模擬一個多人排隊的過程。 #include<cstdio> #include<cstring> #include<algorithm> #i

Week 1 # F Team Queue

ued nbsp input done put arch 接下來 max 不同 題目描述: F - Team Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O

POJ 3481 Double Queue(set實現)

Double Queue   The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment

Python -- queue佇列模組

一 簡單使用 --內建模組哦 import Queuemyqueue = Queue.Queue(maxsize = 10)  Queue.Queue類即是一個佇列的同步實現。佇列長度可為無限或者有限。可通過Queue的建構函式的可選引數maxsize來設定佇列長度。如果maxsize小於1就

資料新增非同步解析重新整理大資料量redis (——)(一)Java Collection之Queue佇列

Queue介面與List、Set同一級別,都是繼承了Collection介面。LinkedList實現了Queue接 口。Queue介面窄化了對LinkedList的方法的訪問許可權(即在方法中的引數型別如果是Queue時,就完全只能訪問Queue介面所定義的方法 了,而不能直接訪問 Linke

C++ 知識回顧總結 -- queue 佇列容器

一、說明 queue 是一種佇列介面卡,專門設計用於FIFO中操作(先進先出),元素從一端插入容器並從另一端提取。 相關API地址為:http://www.cplusplus.com/reference/queue/queue/ 二、使用方法 在C++中只要#include<queue>即

POJ - 3190 貪心+優先佇列

今天,吃飯,被SW教育了一波,堆是完全二叉樹,堆是完全二叉樹,堆是完全二叉樹,重要的事情說三遍。 優先佇列是堆,priority_queue——一個強大的STL,下面先介紹priority_queue的用法。 #include <iostream> #include <

java Queue(佇列)

  佇列是一個典型的先進先出的容器。即從容器的一端放入事物,從另一端取出,並且事物放入容器的順序與取出的順序是相同的。佇列常被當作一種可靠的將物件從程式的某個區域傳輸到另一個區域的途徑。佇列在併發程式設計中特別重要,因為它們可以安全地將物件從一個任務傳輸給另一個任務。   L

SGISTL原始碼閱讀十八 queue(佇列)

SGISTL原始碼閱讀十八 queue(佇列) 前言 和上一篇文章提到過的stack一樣,queue也是一種配接器(adapter),它們的實現非常類似。 它是一種(First In First Out,FIFO)的資料結構,也沒有提供遍歷或指定位置訪問等操作,只能從末端新增元素,頂

【python】詳解queue佇列

一、佇列的定義 佇列類似於一條管道,元素先進先出,進put(arg),取get( )。需要注意的是:佇列都是在記憶體中操作,程序退出,佇列清空,另外,佇列也是一個阻塞的形態。 二、佇列分類 佇列有很多種,但都依賴模組queue 佇列方式

POj-2431 Expedition-優先佇列

問題描述: A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately man

POJ 1821 Fence (單調佇列優化DP)

題目大意:有k個工人,有一排n個磚頭,現在要給磚頭染色,每個工人要麼不染色,要麼選擇一個包含$s_{i}$的,長度不大於$l_{i}$的區域進行染色,然後他們會獲得$len\cdot p_{i}$的報酬,求使所有工人總報酬最大的方案,輸出最大報酬 定義$f[i][j]$表示已經遍歷到了第i個工人,遍歷到了第

Python queue佇列

一、佇列在多執行緒的程式必須安全的在多個執行緒之間互動的時候是非常有用的。   1、 先入先出 1 class queue.Queue(maxsize = 0) 1 #!/usr/bin/python 2 # -*- coding : utf-8 -*- 3 # 作者: Presl