题意:

每个人都属于一个团体,在排队的时候,如果他所在的团体有人在队伍中,则他会站到这个团体的最后。否则站到整个队伍的队尾。

输出每次出队的人的编号。

分析:

容易看出,长队中,在同一个团体的人是排在一起的。

所以用两个队列模拟即可,一个队列保留团体的编号,另外一个队列数组存放的是团体中每个人的编号。

 #include <cstdio>
#include <queue>
#include <map>
using namespace std; const int maxt = + ;
map<int, int> team;
char cmd[]; int main()
{
//freopen("in.txt", "r", stdin); int T, kase = ;
while(scanf("%d", &T) == && T)
{
printf("Scenario #%d\n", ++kase); for(int i = ; i < T; i++)
{
int n, x;
scanf("%d", &n);
while(n--) { scanf("%d", &x); team[x] = i; }
}
queue<int> q, q2[maxt]; //团体队列 和 q2[i]表示团体i中成员的队列 while(scanf("%s", cmd) == )
{
if(cmd[] == 'S') break;
if(cmd[] == 'E')
{
int x, t;
scanf("%d", &x);
t = team[x];
if(q2[t].empty()) q.push(t);
q2[t].push(x);
}
else if(cmd[] == 'D')
{
int t = q.front();
printf("%d\n", q2[t].front());
q2[t].pop();
if(q2[t].empty()) q.pop();
}
}
puts("");
} return ;
}

代码君

05-06 04:59