Team Queue UVA

 题意:有t个团队的人正在排一个长队。每次新来一个人时,如果他有队友在队伍里,那么这个新人会插队到最后一个队友的身后;否则他就排到长队的末尾。

    ENQUEUX x: 编号为x人进入长队。

    DEQUEUX: 长队的队首出队。

    STOP: 停止模拟。

用两个队列,一个是长队,一个是各个团队的队列。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<string>
 5 #include<cstdio>
 6 #include<queue>
 7 #include<map>
 8 using namespace std;
 9 
10 const int maxn=1005;
11 
12 int main()
13 {   int t,T=0;
14     while(scanf("%d",&t)&&t){
15         map<int,int> text;
16         for(int i=0;i<t;i++){
17             int n,m;
18             cin>>n;
19             while(n--){
20                 cin>>m;
21                 text[m]=i;
22             }
23         }
24         printf("Scenario #%d
",++T);
25         queue<int> Q;
26         queue<int> q[maxn];
27         
28         while(true){
29             char a[10];
30             scanf("%s",a);
31             if(a[0]=='S') break;
32             else{
33                 int n;
34                 if(a[0]=='E'){
35                     cin>>n;
36                     if(q[text[n]].size()==0) Q.push(n); 
37                     q[text[n]].push(n);
38                     
39                 }
40                 else{
41                     n=Q.front();
42                     cout<<q[text[n]].front()<<endl;
43                     q[text[n]].pop(); 
44                     if(q[text[n]].empty()) Q.pop();       //The key point!
45                 }
46             }
47         }
48         cout<<endl;
49     }
50     return 0;
51 }
原文地址:https://www.cnblogs.com/zgglj-com/p/7237087.html