Team Queue UVA - 540
有t个团队的人正在排一个长队。每次新来一个人时,如果他有队友在排队,那么这个 新人会插队到最后一个队友的身后。如果没有任何一个队友排队,则他会排到长队的队尾。 输入每个团队中所有队员的编号,要求支持如下3种指令(前两种指令可以穿插进 行)。 ENQUEUEx:编号为x的人进入长队。 DEQUEUE:长队的队首出队。 STOP:停止模拟。 对于每个DEQUEUE指令,输出出队的人的编号。


#include<cstdio> #include<queue> #include<map> #include<iostream> using namespace std; const int maxt = 1000 + 10; int main() { int t,kase = 0; while(cin >> t && t) { printf("Scenario #%d\n",++kase); map<int,int> team; for(int i = 0;i < t;i++) { int n,x; cin >> n; while(n--) { cin >> x; team[x] = i; } } queue<int> q, q2[maxt]; for(;;) { int x; char cmd[10]; cin >> cmd; if(cmd[0] == 'S') break; else if(cmd[0] == 'D') { int t = q.front(); printf("%d\n",q2[t].front()); q2[t].pop(); if(q2[t].empty()) q.pop(); } else if(cmd[0] == 'E') { cin >> x; int t = team[x]; if(q2[t].empty()) q.push(t); q2[t].push(x); } } printf("\n"); } return 0; }

浙公网安备 33010602011771号