题意:给出t个团体,这t个团体排在一起,每次新来一个x排队,如果在整个的团体队列中,有x的队友,那么x排在它的队友的后面,如果他没有队友,则排在长队的队尾

求给出的每一个出队命令,输出出队的人的编号

紫书上的思路:有两个队列,一个是每一个团体内部形成的队列,还有一个是这t个团体又构成的队列

UVa 540 Team Queue 【STL】-LMLPHP

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#define mod=1e9+7;
using namespace std; typedef long long LL;
const int maxn=; int main(){
int t,kase=;
while(scanf("%d",&t)==&&t){
printf("Scenario #%d\n",++kase); map<int,int> team;
for(int i=;i<t;i++){
int n,x;
scanf("%d",&n);
while(n--){
scanf("%d",&x);
team[x]=i;
}
} queue<int> q,q2[maxn];
for(;;){
int x;
char cmd[];
scanf("%s",cmd);
if(cmd[]=='S') break;
else if(cmd[]=='D'){
int t=q.front();
printf("%d\n",q2[t].front());q2[t].pop();
if(q2[t].empty()) q.pop();//如果团体t全部为空,则将团体t整个从团体队列中出去
}
else if(cmd[]=='E'){
scanf("%d",&x);
int t=team[x];
if(q2[t].empty()) q.push(t);//如果团体t现在为空,将它加入团体队列中 。对应于x来的时候,没有一个队友,则他排到长队的队尾
q2[t].push(x);//将编号为x的人加入它应该在的小团队中
}
}
printf("\n");
}
return ;
}
04-20 10:55