题目大意:
有t个团队的人正在排一个长队。每次新来一个人时,如果他有队友在排队,那么新人会插队到最后一个队友的身后。如果没有任何一个队友排队,则他会被排到长队的队尾。 输入每个团队中所有队员的编号,要求支持如下3中指令: ENQUEUE x:编号为x的人进入长队 DEQUEUE:长队的队首出队 STOP:停止模拟 对于每个DEQUEUE指令,输出出队的人的编号。
解题思路:
C++之骚库之一 #include< queue >
还要加一个头文件 #include< cstdlib >
queue 的操作:
queue < int > q;//int类型的队列
q.pop();//队首出队
q.push(x);//将x放入队列
q.front();//返回队首元素
q.back();//返回队尾元素
q.empty();//查看队列是否为空,队列为空则返回true,否则返回false
q.size();//返回队列元素个数
然后就不用手动模拟队列。
我们用t[i]表示i属于哪个团队
队列e表示整个队列
q[i]表示第i个团队有几个人进入队列
Accepted code:
#include<iostream>
#include<stdlib.h>
#include<cstdio>
#include<queue>
using namespace std;
int t[1000000],fk,T,n,x;
char c[8];
int main()
{
while(1)
{
scanf("%d",&T);
if (!T) return 0;
fk++;
queue<int> q[1001];
queue<int> e;
for (int i=1;i<=T;i++)
{
scanf("%d",&n);
while(n--) scanf("%d",&x),t[x]=i;
}
printf("Scenario #%d\n",fk);
while (1)
{
cin>>c;
if (c[0]=='S') {puts("");break;}
if (c[0]=='E')
{
scanf("%d",&x);
if(q[t[x]].empty())
{
e.push(t[x]);
q[t[x]].push(x);
}
else q[t[x]].push(x);
}
else
{
while(q[e.front()].empty())
e.pop();
printf("%d\n",q[e.front()].front());
q[e.front()].pop();
}
}
}
}