我是靠谱客的博主 眯眯眼纸飞机,这篇文章主要介绍算法 团体队列 UVa540 及C++STL之队列回顾,现在分享给大家,希望可以做个参考。

贴上题目链接:UVa540
题目解析:该题目中有两个队列:每个团队有一个队列,而团队整体又形成一个队列。例如,有三个团队编号分别为1,2,3,每个团队的队员集合分别为{101,102,103}、{201,202}、{301、302、303},团队的整体队列为{3,1,2},代码如下:

复制代码
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
#include<iostream> #include<cmath> #include<cstdio> #include<map> #include<algorithm> #include<vector> #include<set> #include<stack> #include<queue> using namespace std; const int maxt=1010; int main() { int t,kase=0; while(cin>>t &&t){ printf("Scenario #%dn",++kase); //记录所有人的团队编号 map<int,int> team; //team[x]表示编号为x的人所在的团队 for(int i=0;i<t;i++){ int n,x; scanf("%d",&n); while(n--){ scanf("%d",&x); team[x]=i; } } //模拟 queue<int> q,q2[maxt]; //q是团队的队列,而q2[i]是团队成员i所在的队列 for(;;){ int x; char cmd[10]; scanf("s",cmd); if(cmd[0]=='S') break; else if(cmd[0]=='D'){ int t=q.front(); printf("%dn",q2[t].front()); q2[t].pop(); if(q2[t].empty()) q.pop();//团体t全体出队列 } else if(cmd[0]=='E'){ scanf("%d",&x); int t=team[x]; if(q2[t].empty()) q.push(t); //团队t进入队列 q2[t].push(x); } } printf("n"); } return 0; }

**队列(Queue)**包含在头文件中,队列也是一种运算受限的线性表,两头都有限制,插入只能在表的一端进行(只进不出),而删除只能在表的另一端进行(只出不进),允许删除的一端称为队尾(rear),允许插入的一端称为队头 (Front),如图所示:队列描述
队列的基本操作:
头文件

push(x) 将x压入队列的末端

pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值

front() 返回第一个元素(队顶元素)

back() 返回最后被压入的元素(队尾元素)

empty() 当队列为空时,返回true

size() 返回队列的长度

最后

以上就是眯眯眼纸飞机最近收集整理的关于算法 团体队列 UVa540 及C++STL之队列回顾的全部内容,更多相关算法内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部