发布题解

lgh  •  2年前


题目分析

涉及到队员编号和团队编号,故可以采用map<int,int>完成队员编号到所在团队编号的映射。采用两个队列来完成模拟即可.

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
int main()
{
  int t,kase=0;
  while(~scanf("%d",&t)&&t)
  {
    printf("Scenario #%d\n",++kase);
    map<int,int> team; //记录所有人的团队编号,team[x]表示编号为x的人所在团队的编号
    for(int i=0; i<t; i++)
    {
      int n,x;
      scanf("%d",&n);
      while(n--)
      {
        scanf("%d",&x);
        team[x]=i;
      }
    }
    //开始模拟
    //q存放队伍,q2存放按序排列的所有队伍及队伍下的编号
    queue<int> q,q2[maxn]; //q是团队的队列,q2[i]是团队i成员的队列
    for(; ;)
    {
      int x;
      char s[10];
      cin>>s;
      if(s[0]=='S')
        break;
      else if(s[0]=='D')
      {
        int t=q.front(); //变量t表示团队整体队列的队首
        printf("%d\n",q2[t].front());
        q2[t].pop(); //输出当前队首队伍的第一个人,并将此人出队列
        if(q2[t].empty())
          q.pop(); //团体t全部出队列
      }
      else if(s[0]=='E')
      {
        scanf("%d",&x);
        int t=team[x]; //通过map映射找到x的队列序号
        if(q2[t].empty())
          q.push(t); //若该队没有排在队伍中,团队t进入队列(队尾)
        q2[t].push(x); //否则把该队列的人插入到q2的该队中
      }
    }
    printf("\n");
  }
  return 0;
}

评论:

add&thx


mod998244353  •  2年前