题目描述
略
样例
略
纯模拟
用一个队列queu< int > g 记录此时队伍中存在的小组 ,用一个队列数组queue< int > q[N],储存各个小组正在排队的情况,用一个数组hsh[m]映射各个人的编号对应的小组。
每次入队出队更新 队列 g 和 对应的队列 q[i]
C++ 代码
#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
using namespace std;
const int N = 1010;
const int M = 1000000;
queue<int> q[N];//索引即对应小组号,坑 ,这个记得每轮结束都清空。
int hsh[M];//编号映射小组
queue<int> g;//小组号队列 , 坑 ,这个记得每轮结束都清空。
int n,k;
void clear(queue<int>& q) {
queue<int> empty;
swap(empty, q);
}
void work()
{
printf("Scenario #%d\n",k);
string odr;
while(cin>>odr&&odr!="STOP")
{
if(odr=="ENQUEUE")
{
int x;
cin>>x;
//如果这个小组队伍暂时还没有人,g[i]加入这个小组编号
if(q[hsh[x]].empty())
{
g.push(hsh[x]);
// cout<<"creat group number :"<<hsh[x]<<" , "<<endl;
}
else
{
// cout<<"group number existed , "<<endl;
}
q[hsh[x]].push(x);
// cout<<x<<"join group :"<<hsh[x]<<endl;
}
else if(odr=="DEQUEUE")
{
cout<<q[g.front()].front()<<endl;
q[g.front()].pop();
if(q[g.front()].empty())
{
// cout<<"clear group " << g.front()<<endl;
g.pop();
}
//如果这时候 g.front() 这个编号 的 小组 的成员 只剩一个人了。那就弹出这个小组编号
}
}
clear(g);
for(int i = 0 ;i <=1001 ;i++)
clear(q[i]);
cout<<endl;
}
int main()
{
while(cin>>n,n)
{
k+=1;
for(int i = 1,t ; i <= n ; i ++)
{
cin>>t;
for(int j = 0 , x ;j < t ; j ++ )
{
//从样例可推测,每个编号都是唯一的。
cin>>x;
hsh[x] = i;//记录对应小组
}
}
work();
}
return 0;
}