Team Queue+队列

原题链接

http://acm.hdu.edu.cn/showproblem.php?pid=1387

思路

这个题是一个经典的队列问题,已知成员在那些组,而入队情况是只告诉一个成员编号,所以需要建立一个映射关系(用一维数组建立编号以组号的关系),入队就用映射关系判断在那个队伍即可。具体看代码

代码
#include<cstdio>
#include<queue>
using namespace std;
const int MAXN=1e3+5;
int arr[MAXN*1000];//arr[x]=i表示x成员在i组里面 
queue<int>team[MAXN];//存储成员 
queue<int>q;//存储组号 
int main()
{
	int t,cnt=1;
	while(scanf("%d",&t)&&t)
	{
		for(int i=0;i<MAXN>>1;i++)	arr[i]=-1;//初始化每个成员最开始没有组
		for(int i=0;i<t;i++)
		{
			int n,x;
			scanf("%d",&n);
			for(int j=0;j<n;j++)
			{
				scanf("%d",&x);
				arr[x]=i;
			}
		}
		printf("Scenario #%d\n",cnt++);
		char str[10];
		while(scanf("%s",&str)&&str[0]!='S')
		{
			if(str[0]=='E')//入队
			{
				int x;
				scanf("%d",&x);
				if(team[arr[x]].empty())	q.push(arr[x]);//判断成员是否为空,如果为空则压入组号,如果不为空,则不进行操作
				team[arr[x]].push(x);//压入成员。 
			} 
			else if(str[0]=='D')//出队 
			{
				printf("%d\n",team[q.front()].front());//把第一组的第一个成员输出 
				team[q.front()].pop();
				if(team[q.front()].empty())	q.pop();//如果第一个组为空,则删除该组 
			} 
		}
		puts("");
		for(int i=0;i<MAXN;i++)//清空容器中的元素,继续进行下一次操作 
			while(!team[i].empty())	team[i].pop();
		while(!q.empty())	q.pop();
	}
	return 0;
}
发布了39 篇原创文章 · 获赞 1 · 访问量 555

猜你喜欢

转载自blog.csdn.net/qq_45249273/article/details/104576936