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 file will contain one or more test cases. Each test case begins with the number of teams t ( ). 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
Miguel A. Revilla
1999-01-11
大意:
有t个小团体,现在模拟一个队列
ENQUEUE x 代表将x压入队列中和它处于同一小团体的尾部,如果队列中没有和它处于同一小团体的元素则将它压入队尾;输出所有弹出的元素;DEQUEUE 代表将队列中的首元素弹出;STOP 代表操作结束;
要点:
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int temp[1005][1005];
int flag[1000000];
int ans[1000000];
int main(){
int t;
int tcount = 0;
while (scanf ("%d", &t) && t != 0){
getchar();
queue<int> que[1005];
queue<int> rank;
memset(temp, -1, sizeof(temp));
for (int i = 0; i < t; i++){
int teamnum;
scanf ("%d", &teamnum);
for (int j = 0; j < teamnum; j++){
scanf ("%d", &temp[i][j]);
flag[temp[i][j]] = i + 1;
}
getchar();
}
char com[10];
int count = 0;
while (scanf ("%s", com) && strcmp(com, "STOP")){
if (!strcmp(com, "ENQUEUE")){
int number;
scanf ("%d", &number);
getchar();
que[flag[number]].push(number);
bool f = true;
for (int i = 0; i < rank.size(); i++){
int tnum = rank.front();
rank.pop();
if (tnum == flag[number])
f = false;
rank.push(tnum);
}
if (f)
rank.push(flag[number]);
}
else{
ans[count++] = que[rank.front()].front();
que[rank.front()].pop();
if (que[rank.front()].empty())
rank.pop();
}
}
printf ("Scenario #%d\n", ++tcount);
for (int i = 0; i < count; i++)
printf ("%d\n", ans[i]);
printf ("\n");
}
return 0;
}
用queue<int> que[1005]将小团体元素存放在属于自己的队列中;
同时使用queue<int> rank,来存放小团体的编号,遍历rank队列,如果有不处理,没有则将小团体编号压入rank;
弹出的时候弹出rank首元素所代表的小团体队列中的首元素,即que[rank.front()].front();
弹出后判断首元素代表的小团体是否为空,为空则rank.pop();
代码: