Queues and Priority Queues aredata structures which are known to most computer scientists. The Team Queue,however, is not so well known, though it occurs often in everyday life. Atlunch 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 (elementsof the same team) are already in the queue. If yes, it enters the queue rightbehind them. If not, it enters the queue at the tail and becomes the new lastelement (bad luck). Dequeuing is done like in normal queues: elements areprocessed 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.
The input file will contain one or more test cases.Each test case begins with the number of teams t ( ). Then t teamdescriptions follow, each one consisting of the number of elements belonging tothe 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. Thereare 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 0for t.
Warning: A test case may contain up to 200000 (two hundredthousand) commands, so the implementation of the team queue should be efficient:both enqueing and dequeuing of an element should only take constant time.
For each testcase, first print a line saying ``Scenario #k",where k is the number of the test case. Then, foreach DEQUEUE command, print the element which is dequeued on a singleline. Print a blank line after each test case, even after the last one.
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
STOP
5 259001 259002 259003 259004 259005
6 260001 260002 260003 260004 260005260006
ENQUEUE 259001
ENQUEUE 260001
ENQUEUE 259002
ENQUEUE 259003
ENQUEUE 259004
ENQUEUE 259005
DEQUEUE
DEQUEUE
ENQUEUE 260002
ENQUEUE 260003
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0
101
102
103
201
202
203
Scenario #2
259001
259002
259003
259004
259005
260001
代码:
#include<iostream>
#include<queue>
#include<map>
using namespacestd;
int main()
{
int test1;
int time=1;
while(cin>>test1&&test1!=0)
{
map<int,int> team;
queue<int> q,q2[1010];//不要声明为全局变量
cout<<"Scenario#"<<time<<endl;
time++;
for(int i=0; i<test1; i++)
{
int num;
cin>>num;
while(num--)
{
int a;
cin>>a;
team[a]=i;
}
}
string s;
while(cin>>s&&s!="STOP")
{
if(s=="ENQUEUE")
{
int num;
cin>>num;
int x=team[num];
if(q2[x].empty())
{
q.push(x);
}
q2[x].push(num);
}
else
{
int num=q.front();
cout<<q2[num].front()<<endl;
q2[num].pop();
if(q2[num].empty())
{
q.pop();
}
}
}
cout<<endl;
}
}
解析:
题意是有若干团队,每个团队有若干元素.向一个队列中插入元素,如果有和将要插入的元素在同一团体的元素已经在队列中,则将该元素插入到该团体元素的最后面;否则,将该元素中插入到整个队列的最后面。用一个队列无法实现该功能。因此:用两层队列。每个团体各自有一个队列,而所有的团体又形成一个队列。容器的功能如下:
map<int,int>team:第一的int存取实际元素的值,根据输入,将一组的所有元素映射成同一个值(用映射搜索起来很快、方便),该值就是团队的编号
queue<int>q,q2[1010]:q是第一层队列:团体编号的队列,插入的是map中的映射值;q2是第二层队列数组,每个元素是一个队列,插入对应团体编号的元素。