題解:用一個map存儲每個号碼的隊伍号,然後再用二維隊列存放每個隊伍的進場号碼,最後再用一個隊列存儲已經在排隊的隊伍号,隊伍空了就pop掉,下一個隊伍補上。
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
using namespace std;
int main() {
int t, n, a, cases = 1, flag;
queue<int> q[1005];
queue<int> q2;
char str[10];
while (scanf("%d", &t) && t) {
map<int,int> m;
for (int i = 1; i <= t; i++) {
scanf("%d", &n);
for (int j = 1; j <= n; j++) {
scanf("%d", &a);
m[a] = i;
}
}
printf("Scenario #%d\n", cases++);
while (scanf("%s", str)) {
if (str[0] == 'S')
break;
else if (str[0] == 'E') {
scanf("%d", &a);
if (q[m[a]].empty())
q2.push(m[a]);
q[m[a]].push(a);
}
else {
flag = q2.front();
printf("%d\n", q[flag].front());
q[flag].pop();
if (q[flag].empty()) {
q2.pop();
}
}
}
printf("\n");
for (int i = 1; i <= t; i++)
while (!q[i].empty())
q[i].pop();
while (!q2.empty())
q2.pop();
}
return 0;
}