题目

文章目录

思路:

紫书

收获

原来队列还可以这样用,对于每一个q2[i]都是一个队列

   queue<int> q2[maxt];

Ac代码

#include <iostream>
#include<queue>
#include<map>
using namespace std;
const int maxt = 1000+9;
int main()
{
   int t;
   int cnt= 0;
   while(cin>>t)//模拟有多少个队伍
   {
       if(!t) break;
       map<int,int> team;//team[x] 代表队员x所在的队伍
       for(int i=0;i<t;i++)
       {
           int n,x;//第i+1个队伍里有多少个人
           cin>>n;
           while(n--) {cin>>x ; team[x] = i; }
       }
       printf("Scenario #%d\n",++cnt);
       queue<int> q1,q2[maxt];//一定要定义在while 循环外面
       while(1)
       {
            //第一个代表的是团队编号的队列
       //第二个代表每一个团队里的人
       string cmd;//command
       cin>>cmd;
       if(cmd[0]=='E')
       {
           int x;//队员
           cin>>x;
           int q = team[x];//代表这个队员所在的队伍编号
           //队伍q还没有进长队
           if(q2[q].empty()) q1.push(q);
           q2[q].push(x);
       }
       else if(cmd[0]=='D')
       {
           int q = q1.front();
            int ans = q2[q].front();
            printf("%d\n",ans);
            q2[q].pop();
            //如果队伍q里面没人了
           //那么直接把这个队伍编号从q1中弹出
           if(q2[q].empty()) q1.pop();

       }
      else  if(cmd[0]=='S') break;
       }
       cout<<endl;

   }
    return 0;
}