POJ 2259 Team Queue【模擬佇列】
Team Queue
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 4449 | Accepted: 1541 |
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.
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:
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.
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
AC程式碼:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int a[1011*1000];
int main()
{
int T,Kase=0;
while(~scanf("%d",&T),T) {
for(int i=1;i<=T;++i) {
int s; scanf("%d",&s);
for(int j=1;j<=s;++j) {
int tem=0; scanf("%d",&tem);
a[tem]=i;
}
}
queue<int> Q[T+1];
queue<int> qm;
char str[22]; printf("Scenario #%d\n",++Kase);
while(~scanf("%s",str),str[0]!='S') {
if(str[0]=='D') {
int F=qm.front();
int y=Q[F].front(); Q[F].pop();
printf("%d\n",y);
if(Q[F].empty()) qm.pop();
}
else {
int x; scanf("%d",&x);
if(Q[a[x]].empty()) qm.push(a[x]);
Q[a[x]].push(x);
}
}
puts("");
}
return 0;
}
相關文章
- Team Queue(佇列)佇列
- Team Queue (佇列的一種應用)佇列
- Team Queue
- python佇列QueuePython佇列
- JDK QUEUE佇列JDK佇列
- 【Java程式設計】使用Java模擬C/C++中的queue佇列Java程式設計C++佇列
- C# 佇列(Queue)C#佇列
- 1284 海港 普及組 NOIP2016 佇列基礎 簡單列舉 簡單模擬 優先佇列(priority_queue)佇列
- 陣列模擬佇列 以及佇列的複用(環形佇列)陣列佇列
- 佇列(Queue)-c實現佇列
- 高效能佇列 Aeron Queue vs Chronicle Queue佇列
- laravel原始碼分析-佇列QueueLaravel原始碼佇列
- js資料結構--佇列(queue)JS資料結構佇列
- java Queue佇列相關總結Java佇列
- 資料結構之佇列(Queue)資料結構佇列
- POJ——1016Parencodings(模擬)Encoding
- Throwing cards away I(queue迴圈佇列)佇列
- python的queue佇列獲取資料Python佇列
- 陣列模擬佇列進階版本——環形佇列(真正意義上的排隊)陣列佇列
- 模擬銀行排隊時間—4佇列佇列
- java集合類——Stack棧類與Queue佇列Java佇列
- C++ STL 優先佇列 (priority_queue)C++佇列
- 【資料結構】棧(Stack)和佇列(Queue)資料結構佇列
- 看動畫學演算法之:佇列queue動畫演算法佇列
- CSP之公共鑰匙盒(模擬、排序、優先佇列)排序佇列
- 分散式任務 + 訊息佇列框架 go-queue分散式佇列框架Go
- Sunscreen POJ - 3614(防曬油) 貪心-優先佇列佇列
- POJ3087 Shuffle'm Up【簡單模擬】
- Python3 執行緒優先順序佇列( Queue)Python執行緒佇列
- 【FreeRtos教程三】STM32 CubeMx——Message Queue(訊息佇列)佇列
- 原始碼解析Synchronous Queue 這種特立獨行的佇列原始碼佇列
- 佇列Queue:任務間的訊息讀寫,安排起來~佇列
- time模組,collections模組,佇列和棧佇列
- 7.27考試總結(NOIP模擬25)[random·string·queue]random
- Stack and Queue in JavaScript(Javascript中的資料結構之棧和佇列)JavaScript資料結構佇列
- 使用Chronicle Queue建立低延遲的TB級別的佇列 - DZone佇列
- leetcode 232. Implement Queue using Stacks 用棧實現佇列(簡單)LeetCode佇列
- T9-POJ 1415 模擬輸入法+字典樹
- 優先佇列的學習記錄--例題:Expedition(POJ2431)佇列