维护一个队列,队列的元素是一个队列。我们定义队列元素为短队,队列为长队。长队的元素是短队的索引,短队的元素是队员号。
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
const int MAXN = 1e3 + 10;
int main() {
int n;
int cnt = 0;
while (~scanf("%d", &n) && n) {
int m, x;
map<int, int> team; // 记录队员到队伍的映射
for (int i = 0; i < n; ++i) {
scanf("%d", &m);
for (int j = 0; j < m; ++j) {
scanf("%d", &x);
team[x] = i;
}
}
// q1 长队,q2 所有短队
queue<int> q1, q2[MAXN];
char s[10];
printf("Scenario #%d\n", ++cnt);
while (1) {
scanf("%s", s);
if (s[0] == 'S') break;
else if (s[0] == 'E') { // 入队
scanf("%d", &x);
int t = team[x]; // 拿到索引
if (q2[t].empty()) { // 如果该队伍是空的,那么长队中没有该队伍,应该加入队伍索引
q1.push(t);
}
q2[t].push(x); // 短队元素入队
} else if (s[0] == 'D') { // 出队
int t = q1.front(); // 拿到长队队首索引
printf("%d\n", q2[t].front());
q2[t].pop(); // 短队元素出队
if (q2[t].empty()) { // 如果短队为空,从长队中出队
q1.pop();
}
}
}
printf("\n");
}
return 0;
}