對於每個團隊,各自有一個 Queue 管理各個團隊人員的順序,接著再利用一個 Queue 去決定這些團隊的順序即可。
C++(0.060)
/*******************************************************/
/* UVa 540 Team Queue */
/* Author: Maplewing [at] knightzone.studio */
/* Version: 2019/04/21 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <vector>
#include <queue>
using namespace std;
int main(){
int t;
int caseNumber = 1;
while(scanf("%d", &t) != EOF && t > 0){
map<int, int> teamIndexMap;
int n;
for(int teamIndex = 0 ; teamIndex < t ; ++teamIndex){
scanf("%d", &n);
int member;
for(int i = 0 ; i < n ; ++i){
scanf("%d", &member);
teamIndexMap[member] = teamIndex;
}
}
printf("Scenario #%d\n", caseNumber++);
vector<queue<int>> individualTeamQueue(t);
queue<queue<int>*> teamQueue;
string command;
int x;
while(cin >> command && command != "STOP"){
if(command == "ENQUEUE"){
scanf("%d", &x);
int teamIndex = teamIndexMap[x];
if(individualTeamQueue[teamIndex].empty()){
teamQueue.push(&individualTeamQueue[teamIndex]);
}
individualTeamQueue[teamIndex].push(x);
}
else if(command == "DEQUEUE"){
queue<int>* firstTeamQueue = teamQueue.front();
printf("%d\n", firstTeamQueue->front());
firstTeamQueue->pop();
if(firstTeamQueue->empty()) teamQueue.pop();
}
}
printf("\n");
}
return 0;
}