概述
题目描述:
Problem 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:
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.
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.
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
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
101
102
103
201
202
203
Scenario #2
259001
259002
259003
259004
259005
260001
这一题就是我们昨天比赛的原题
TJU1168
刚开始用链表做的,指来指去都迷了。后来队友说可以用队列实现,学到了队列数组的知识,呵呵,欣慰!
先把每个团体对应一个标号,用该标号对应队列数组。
然后元素入队时先入队列数组,如果该队列没在总的队列中,就把该队列的标号压入总队列,对队列的标号进行标记。
出队是出的是总队列队首标号指向的队列数组中的队列的队首元素,如果出队后该队列为空,总队列队首出队,该队列的标号恢复为未标记。
代码
#include
<
iostream
>
#include < queue >
#include < string >
#include < map >
#pragma warning (disable:4786)
using namespace std;
int main()
{
int i,n,m,t,ca = 1 ;
bool visit[ 1002 ];
char str[ 20 ];
while (scanf( " %d " , & n),n)
{
queue < int > q[ 1002 ],que;
map < int , int > team;
for (i = 0 ;i < n;i ++ )
{
scanf( " %d " , & m);
while (m -- )
{
scanf( " %d " , & t);
team[t] = i;
}
}
memset(visit, 0 , sizeof (visit));
printf( " Scenario #%dn " ,ca ++ );
while (scanf( " %s " ,str))
{
if (strcmp(str, " STOP " ) == 0 )
{
puts( "" );
break ;
}
else if (strcmp(str, " ENQUEUE " ) == 0 )
{
scanf( " %d " , & t);
q[team[t]].push(t);
if (visit[team[t]] == false )
{
que.push(team[t]);
visit[team[t]] = true ;
}
}
else
{
printf( " %dn " ,q[que.front()].front());
q[que.front()].pop();
if (q[que.front()].empty())
{
visit[que.front()] = false ;
que.pop();
}
}
}
}
return 0 ;
}
#include < queue >
#include < string >
#include < map >
#pragma warning (disable:4786)
using namespace std;
int main()
{
int i,n,m,t,ca = 1 ;
bool visit[ 1002 ];
char str[ 20 ];
while (scanf( " %d " , & n),n)
{
queue < int > q[ 1002 ],que;
map < int , int > team;
for (i = 0 ;i < n;i ++ )
{
scanf( " %d " , & m);
while (m -- )
{
scanf( " %d " , & t);
team[t] = i;
}
}
memset(visit, 0 , sizeof (visit));
printf( " Scenario #%dn " ,ca ++ );
while (scanf( " %s " ,str))
{
if (strcmp(str, " STOP " ) == 0 )
{
puts( "" );
break ;
}
else if (strcmp(str, " ENQUEUE " ) == 0 )
{
scanf( " %d " , & t);
q[team[t]].push(t);
if (visit[team[t]] == false )
{
que.push(team[t]);
visit[team[t]] = true ;
}
}
else
{
printf( " %dn " ,q[que.front()].front());
q[que.front()].pop();
if (q[que.front()].empty())
{
visit[que.front()] = false ;
que.pop();
}
}
}
}
return 0 ;
}
然后下一步该主攻《数论》了,要加油打个漂亮仗啊!!
转载于:https://www.cnblogs.com/DreamUp/archive/2010/07/22/1783004.html
最后
以上就是苗条芹菜为你收集整理的HDU1387 Team Queue的全部内容,希望文章能够帮你解决HDU1387 Team Queue所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复