source:
题意:就是模拟平时食堂排队,学生分为不同的组,然后每来一个学生先从前往后看有没有自己一个组的,有的话就插列插到自个儿组最后一个。现在给出分组名单然后进行多次询问,输出每次出队询问时出队的学生编号。
思路:对于每个组分别用一个queue模拟,然后组号也进行queue模拟即可
关于queue的用法:
头文件:#include<queue>
声明:queue<int> q
基本操作:
- q.push(x) 将x加到队尾
- q.pop() 将队头第一个元素弹出 注意:并不返回该元素的值
- q.front() 返回队头元素值
- q.back() 返回对尾元素值
- q.empty(),当队列空时,返回true
- q.size() 返回队列中元素的个数
代码如下:
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int belong[1000005],isinq[1005];
queue<int> seq;
queue<int> q[1005];
int main()
{
int t,n,team,x,T=1;
char s[10];
while(scanf("%d",&t)!=EOF && t!=0)
{
printf("Scenario #%d\n",T++);
memset(belong,0,sizeof(belong));
memset(isinq,0,sizeof(isinq));
for(int i=1;i<=t;i++) while(!q[i].empty()) q[i].pop();
while(!seq.empty()) seq.pop();
for(int i=1;i<=t;i++)
{
scanf("%d",&n);
for(int j=0;j<n;j++)
{
scanf("%d",&x);
belong[x]=i;
}
}
while(scanf("%s",s)!=EOF && s[0]!='S')
{
if(s[0]=='E')
{
scanf("%d",&x);
team=belong[x];
if(isinq[team]==0)
{
seq.push(team);
isinq[team]=1;
}
q[team].push(x);
}
else
{
team=seq.front();
printf("%d\n",q[team].front());
q[team].pop();
if(q[team].empty())
{
isinq[team]=0;
seq.pop();
}
}
}
printf("\n");
}
return 0;
}