UVa - 540 Team Queue
题意
有一个长队包含几个小队, 每个小队连在一起排成一个长队, 有以下三种操作 :
① ENQUEUE 在队列中输入元素x
② DEQUEUE 输出长队队首并将其从队列中移除
③ STOP 结束处理如果有队友就站到自己所属队列的最末, 如果没有队友就站到整个长队的最末
AC代码
#include <iostream>
#include <cstdio>
#include <map>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 1000 + 10;
int main()
{
int q,x;
int casenum = 0;
while( ~scanf("%d",&q) && q )
{
printf("Scenario #%d\n", ++casenum);
map<int, int> team;
for( int i = 1; i <= q; i++ )
{
int n;
scanf("%d",&n);
while( n-- ){
scanf("%d",&x);
team[x] = i; //记录编号为x的人所在的队伍为第i队
//printf("%d,",team[x]);
}
}
queue<int> q, sq[maxn];
//q是长队的队列,sq[i]是小队i成员的队列
for(;;)
{
char s[maxn];
scanf("%s",s);
int mrk = s[0];
if( mrk == 'S' )
{
puts(""); //Print a blank line
after each test case, even after the last one.
break;
}
else if( mrk == 'E' )
{
scanf("%d", &x);
int t = team[x];
if(sq[t].empty())
q.push(t); //团队t进入队列
sq[t].push(x);
}
else if( mrk == 'D' )
{
int t = q.front();
printf("%d\n", sq[t].front());
sq[t].pop();
if(sq[t].empty())
q.pop(); //团体t全体出队列
}
}
}
return 0;
}