原题链接:https://vjudge.net/problem/UVA-540
分类:<queue>
备注:queue与STL其他容器的综合运用
思路
看题目标题一般都会考虑队列的,题目内容确实也是排队。
已知队伍数目上限1000,那么开数组把不同队伍分开是可以接受的。
用queue数组存取在排队的人,每次新来一个人找已用过的queue中是否有队友,有则加入队伍,无则再用一个queue存入新来的。这样每个队伍已经根据数组的下标排好了顺序,每当前面队伍的人全走完了才会找下一个队伍的人,即可符合题目要求。
代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54#include<iostream> #include<string> #include<queue> #include<map> using namespace std; const int maxt = 1000 + 5; int t, n, x, kase; int main(void) { while (~scanf("%d", &t) && t) { printf("Scenario #%dn", ++kase); map<int, int>team; for(int i=0;i<t;i++) { scanf("%d", &n); for (int j = 0; j < n; j++) { scanf("%d", &x); team[x] = i; } } string cmd; queue<int>wait[maxt]; int now = 0, cnt = 0; while (cin >> cmd) { if (cmd[0] == 'S')break; else if (cmd[0] == 'E') { scanf("%d", &x); int ok = 0; for (int i = now; i < cnt; i++) if (!wait[i].empty() && team[wait[i].front()] == team[x]) { wait[i].push(x); ok = 1; break; } if (!ok)wait[cnt++].push(x); } else if (cmd[0] == 'D') { if (wait[now].empty()) now++; printf("%dn", wait[now].front()); wait[now].pop(); } } printf("n"); } return 0; }
最后
以上就是怡然唇膏最近收集整理的关于例题5-6 团体队列(Team Queue,UVa 540)的全部内容,更多相关例题5-6内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复