把一个大的队列分成几个小队列,大队列里放的是队列的编号,小队列里放的是成员的编码。进行ENQUEUE
操作时,要看大队列中是否有这个成员所在小队列即该小队列是否为空。若为空则向大队列push小队列编号(并向小队列push该成员),否则直接向小队列push该成员(模拟插队)。
注:用map构建成员到team编号的映射。
#include <map>
#include <iostream>
#include <queue>
#include <sstream>
using namespace std;
const int maxn = 1000 + 5;
map<int, int> team; // 成员 to Team
int main() {
//freopen("input.txt", "r", stdin);
int T;
int kase = 0;
while(scanf("%d", &T) != EOF && T) {
cout << "Scenario #" << ++kase << endl;
queue<int> qu, quteam[maxn];
team.clear();
for(int i = 0; i < T; i++) {
int n;
cin >> n;
for(int j = 0; j < n; j++) {
int t;
cin >> t;
team[t] = i;
}
}
string op;
while(cin >> op) {
if (op[0] == 'S') break;
else if(op[0] == 'E') {
int mem;
cin >> mem;
int t = team[mem];
if (quteam[t].empty()) qu.push(t);
quteam[t].push(mem);
}
else {
int t = qu.front();
cout << quteam[t].front() << endl;
quteam[t].pop();
if (quteam[t].empty()) qu.pop();
}
}
cout << endl;
}
return 0;
}