团体队列(Team Queue,UVa540)

有t个团队的人正在排一个长队。每次新来一个人时,如果他有队友在排队,那么这个 新人会插队到最后一个队友的身后。如果没有任何一个队友排队,则他会排到长队的队尾。 输入每个团队中所有队员的编号,要求支持如下3种指令(前两种指令可以穿插进行)
ENQUEUEx:编号为x的人进入长队。
DEQUEUE:长队的队首出队。
STOP:停止模拟。
对于每个DEQUEUE指令,输出出队的人的编号。
分析 首先每个团队是一个队列 团队整体是一个队列
因为有团队,队员 所以有映射 考虑用map

#include<iostream>
#include<queue>
#include<map>
using namespace std;
const int maxt = 1000+10;
int main()
{
	int t,kase=0;
	while(cin>>t&&t)
	{
		printf("Scenario #%d\n", ++kase);
		//记录所有人的团队编号
		map<int,int> team; //team[x]表示编号为x的人所在的团队编号
		for(int i=0;i<t;i++)
		{
			int n,x;
			cin>>n; //队员数量
			while(n--)
			{
				cin>>x
				team[x]=i;
			}  
		}
		
		queue<int> q; //团队整体的队列
		queue<int> q2[maxt];  //团队i的成员的队列 
		for(;;)
		{
			int x;
			char cmd[10];
			scanf("%s",cmd);
			if(cmd[0]=='S') break;
			else if (cmd[0]=='D')
			{
				int t=q.front();
				cout<<q2[t].front()<<endl;  q2[t].pop();
				
				if(q2[t].empty()) q.pop(); //团体t全体出队列
			}
			
			else if(cmd[0] == 'E') 
			{ 
				scanf("%d", &x); 
				int t = team[x]; 
				if(q2[t].empty()) 
				q.push(t); //团队t进入队列 q2[t].push(x);
			}
			cout<<endl;
		} 
	}
	return 0;
} 
 posted on 2020-04-06 20:12  My_serendipity  阅读(446)  评论(0编辑  收藏  举报