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 (佇列的一種應用)佇列
- 【Java程式設計】使用Java模擬C/C++中的queue佇列Java程式設計C++佇列
- JDK QUEUE佇列JDK佇列
- 陣列模擬佇列 以及佇列的複用(環形佇列)陣列佇列
- python佇列QueuePython佇列
- C# 佇列(Queue)C#佇列
- 佇列(Queue)-c實現佇列
- STL(十九)queue佇列容器佇列
- laravel原始碼分析-佇列QueueLaravel原始碼佇列
- 資料結構之佇列(Queue)資料結構佇列
- java Queue佇列相關總結Java佇列
- js資料結構--佇列(queue)JS資料結構佇列
- POJ 2823 單調佇列佇列
- 模擬銀行排隊時間—4佇列佇列
- 陣列模擬佇列進階版本——環形佇列(真正意義上的排隊)陣列佇列
- 29_檢查點佇列(checkpoint queue)佇列
- POJ 3253 Fence Repair 優先佇列AI佇列
- java集合類——Stack棧類與Queue佇列Java佇列
- Throwing cards away I(queue迴圈佇列)佇列
- 【資料結構】棧(Stack)和佇列(Queue)資料結構佇列
- 看動畫學演算法之:佇列queue動畫演算法佇列
- C++ STL 優先佇列 (priority_queue)C++佇列
- STL(二十)priority_queue優先佇列容器佇列
- Python3 queue佇列模組詳解Python佇列
- 訊息佇列(Message Queue)基本概念佇列
- [筆記] 解碼Nginx:雙向佇列(Queue)筆記Nginx佇列
- jQuery佇列控制方法詳解queue()/dequeue()/clearQueue()jQuery佇列
- POJ 1724 ROADS(優先佇列+spfa)佇列
- POJ 3017 單調佇列dp佇列
- POJ2431 Expedition (優先佇列)佇列
- POJ 2051(最小堆/優先佇列)佇列
- Parencodings(POJ 1068)(模擬)Encoding
- CSP之公共鑰匙盒(模擬、排序、優先佇列)排序佇列
- 分散式任務 + 訊息佇列框架 go-queue分散式佇列框架Go
- poj3017 dp+單調佇列佇列
- POJ——1016Parencodings(模擬)Encoding
- POJ 3106Flip and Turn(模擬)
- 原始碼解析Synchronous Queue 這種特立獨行的佇列原始碼佇列