UVa 540 (团体队列) Team Queue

题意:

每个人都属于一个团体,在排队的时候,如果他所在的团体有人在队伍中,则他会站到这个团体的最后。否则站到整个队伍的队尾。

输出每次出队的人的编号。

分析:

容易看出,长队中,在同一个团体的人是排在一起的。

所以用两个队列模拟即可,一个队列保留团体的编号,另外一个队列数组存放的是团体中每个人的编号。

 1 #include <cstdio>
 2 #include <queue>
 3 #include <map>
 4 using namespace std;
 5 
 6 const int maxt = 1000 + 10;
 7 map<int, int> team;
 8 char cmd[10];
 9 
10 int main()
11 {
12     //freopen("in.txt", "r", stdin);
13 
14     int T, kase = 0;
15     while(scanf("%d", &T) == 1 && T)
16     {
17         printf("Scenario #%d
", ++kase);
18 
19         for(int i = 0; i < T; i++)
20         {
21             int n, x;
22             scanf("%d", &n);
23             while(n--) { scanf("%d", &x); team[x] = i; }
24         }
25         queue<int> q, q2[maxt];   //团体队列 和 q2[i]表示团体i中成员的队列
26 
27         while(scanf("%s", cmd) == 1)
28         {
29             if(cmd[0] == 'S') break;
30             if(cmd[0] == 'E')
31             {
32                 int x, t;
33                 scanf("%d", &x);
34                 t = team[x];
35                 if(q2[t].empty()) q.push(t);
36                 q2[t].push(x);
37             }
38             else if(cmd[0] == 'D')
39             {
40                 int t = q.front();
41                 printf("%d
", q2[t].front());
42                 q2[t].pop();
43                 if(q2[t].empty()) q.pop();
44             }
45         }
46         puts("");
47     }
48 
49     return 0;
50 }
代码君
原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4251458.html