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\n", ++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\n", 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值