STL--queue
queue-概述:
队列中没有元素时,称为空队列。
bool empty() |
队列为空返回true,否则返回false |
void pop() |
删除队列的一个元素 |
void push( const TYPE &val ) |
将val元素加入队列 |
size_type size() |
返当前队列中的元素数目 |
TYPE &back() |
返回一个引用,指向队列的最后一个元素 |
TYPE &front() |
返回队列第一个元素的引用 |
bool empty() |
优先队列为空返回true,否则返回false |
void pop() |
删除优先队列中的第一个元素 |
void push( const TYPE &val ) |
添加一个元素到优先队列中,值为val |
size_type size() |
返当前队列中的元素数目 |
TYPE &top () |
返回一个引用,指向最高优先级的元素 |
1 #include<iostream> 2 #include<queue> 3 #include<set> 4 #include<vector> 5 using namespace std; 6 7 void parse(vector<set<int> >&adj, unsigned int p=0)//十分巧妙地递归读入。 8 { 9 unsigned int x; 10 cin>>x; 11 if(p) 12 { 13 adj[p].insert(x); 14 adj[x].insert(p); 15 } 16 while(true) 17 { 18 char ch; 19 cin>>ch; 20 if(ch==')') break; 21 parse(adj, x); 22 } 23 return; 24 } 25 26 int main() 27 { 28 //freopen( "in.txt", "r", stdin ); 29 //freopen( "out.txt", "w", stdout ); 30 char ch; 31 while(cin>>ch) 32 { 33 vector<set<int> > adj(1024, set<int>()); 34 parse(adj); 35 priority_queue<int, vector<int>, greater<int> >leafs; 36 int n = 0; 37 for(unsigned int i = 0; i<adj.size();i++) 38 if(adj[i].size()) 39 { 40 n++; 41 if(adj[i].size()==1) 42 leafs.push(i); 43 } 44 for(int k=1; k<n; k++) 45 { 46 unsigned int x = leafs.top(); 47 leafs.pop(); 48 unsigned int p = *(adj[x].begin()); 49 if(k>1) 50 cout<<" "; 51 cout<<p; 52 adj[p].erase(x); 53 if(adj[p].size()==1) 54 leafs.push(p); 55 } 56 cout<<endl; 57 } 58 return 0; 59 }
View Code
#include<algorithm> #include<vector> #include<queue> #include<cstdio> #include<iostream> using namespace std;int main() {int n, k;while(~scanf("%d%d", &n, &k)){priority_queue<int, vector<int>, greater<int> >que;while(n--){char op[5];scanf("%s", op);if(op[0]=='I'){int val;scanf("%d", &val);que.push(val);while(que.size()>k) que.pop();}else printf("%d\n", que.top());}}return 0; }
View Code
(提醒:不要直接交代码, 上面代码不能通过杭电的编译器)。
然后稍加改变, 编写自己的比较函数,而不是用自带的算子(greater)。 结果就过啦! 也是十分的蛋疼,十分的无语!!!。
#include<algorithm> #include<vector> #include<queue> #include<cstdio> #include<iostream> using namespace std;struct Cmp{bool operator()(const int&t1, const int&t2){return t1>t2;}};int main() {int n, k;while(~scanf("%d%d", &n, &k)){priority_queue<int, vector<int>,Cmp>que;while(n--){char op[5];scanf("%s", op);if(op[0]=='I'){int val;scanf("%d", &val);que.push(val);while(que.size()>k) que.pop();}else printf("%d\n", que.top());}}return 0; }
View Code
上面这个代码也不太好, 因为它有了太多的进队和出队, 每一次的进出都是需要维护优先队列的, 所以可以在入队满K个后, 在后来的插入时, 可以先比较一下, 然后决定是否插入。
#include<algorithm> #include<vector> #include<queue> #include<cstdio> #include<iostream> using namespace std;struct Cmp{bool operator()(const int&t1, const int&t2){return t1>t2;}};int main() {int n, k;while(~scanf("%d%d", &n, &k)){priority_queue<int, vector<int>,Cmp>que;int t = k;char op[5];int val;while(t--){scanf("%s", &op);scanf("%d", &val);que.push(val);}n = n-k;while(n--){scanf("%s", &op);if(op[0]=='I'){scanf("%d", &val);if(val>que.top())que.push(val), que.pop();}else printf("%d\n", que.top());}}return 0; }
View Code
一般优先队列的定义方法:
priority_queue< Type,vector<Type>,greater<Type> >(小顶堆)
priority_queue< Type,vector<Type>,less<Type> >(大顶堆)
如果是自定义的结构体类型
priority_queue<Node,vector<Node>,cmp>
需要自己自定义结构体类型:
struct cmp
{
bool operator()(const Node &t1,const Node &t2)
{
return t1.b<t2.b;//相当于less,大顶堆
}
};
3.又是一道可以用优先队列解的题。 优先队列的功能还真强大。
http://acm.hdu.edu.cn/showproblem.php?pid=4393
#include<cstdio> #include<iostream> #include<queue> #include<algorithm> using namespace std;struct Node{int F;int index;friend bool operator<(Node a, Node b){if(a.F!=b.F) return a.F<b.F;else return a.index>b.index;} };priority_queue<Node>q[110]; int main() {int T;int kase, n, S;Node a;scanf("%d", &T);for(kase=1; kase<=T; kase++){scanf("%d", &n);for(int i=1; i<=n; i++){scanf("%d%d", &a.F, &S);a.index = i;q[S].push(a);}printf("Case #%d:\n", kase);for(int i=0; i<n; i++){int fast = -1, id = 1;for(int j=1; j<=100; j++)if(!q[j].empty()){Node tmp=q[j].top();if(tmp.F+i*j>fast) fast=tmp.F+i*j, id=j;else if(tmp.F+i*j==fast&&tmp.index<q[id].top().index) id = j;}printf("%d", q[id].top().index);q[id].pop();if(i<n-1)printf(" ");else printf("\n");}}return 0; }
View Code
转载于:https://www.cnblogs.com/acm1314/p/4540627.html
STL--queue相关推荐
- stl:queue 源码_C ++ STL中的queue :: empty()和queue :: size()
stl:queue 源码 In C++ STL, Queue is a type of container that follows FIFO (First-in-First-out) element ...
- STL—queue(队列) 详解
首先,在STL中 queue 和 stack 其实并不叫容器(container),而是叫适配器(adapter),他们是对容器的再封装. 队列queue: 队列,简称对,是一种操作受限的线性表.限制 ...
- STL——queue、priority_queue、deque
文章目录 queue(队列) priority_queue(优先队列) 1.与queue的区别: 2.示例: 3.priority_queue内元素优先级设置 deque 示例: queue(队列) ...
- STL queue priority_queue
queue 队列 queue 队列 就像排队一样,后来的排在后面,先来的先出队.所以只能对队首和队尾的元素进行操作 back() 返回最后一个元素 empty() 如果队列空则返回真 front() ...
- STL - queue(队列)
Queue简单介绍 queue是队列容器.是一种"先进先出"的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> qu ...
- STL queue:男孩被绑架了!
题目描述 男孩被绑架了! 他非常想念他的母亲,现在非常害怕.你想象不出他被关在的房间有多黑,这么穷吗?. 作为一个聪明的ACboy,你想把ACboy从怪物的迷宫里救出来,但是当你来到迷宫的门口时,怪物 ...
- [C++STL]queue容器用法介绍
- Queue+Stack(C++,标准库中的队列和栈)
STL Queue(先进先出) 头文件#include 成员函数 queue::pop () //出队列 queue::push () //入队列 queue::size () //返回队列中元素个数 ...
- STL容器底层数据结构的实现
C++ STL 的实现: 1.vector 底层数据结构为数组 ,支持快速随机访问 2.list 底层数据结构为双向链表,支持快速增删 3.deque 底层 ...
- c++ STL 容器
STL源码分析 (一)vector容器 vector的数据安排以及操作方式,与array非常相似.两者的唯一区别在于空间的运用的灵活性.array是静态空间,一旦配置了就不能改变.vector是动态空 ...
最新文章
- iOS动画进阶 - 手摸手教你写ShineButton动画
- opencv滤波美颜
- Hello World!!
- python模块 init py_Python模块包中__init__.py文件的作用
- 唯一标识计算机的位置目录与文件的是,计算机应用基础 7.doc
- Java 文件 IO 操作
- 60 MM配置-后勤发票校验-设置重复发票检查
- Windows Phone 8.1 多媒体(3):音乐
- 微信生成专属海报(专属二维码)
- spyder python下载_Spyder python下载
- cmdn发布博客(markdowm)
- 解读《美国国家BIM标准》– BIM能力成熟度模型(六)
- python词性标注_Python NLTK学习5(词性标注)
- 距阵乘以一个未知距阵得单位矩阵 怎么算_干货分享:怎样假装一个带货流水过亿的直播达人?...
- Assignment | 05-week3 -Part_1-Neural Machine Translation
- 手游测试(测试内容、测试流程、测试用例)
- 老男孩MysqlDBA专家班
- 【编程题】【Scratch三级】2022.06 五彩糖葫芦
- 尼康D7100_认识机身上的控件
- arduino 舵机接线图_arduino+16路舵机驱动板连接测试(示例代码)
热门文章
- webpack使用七
- J - X and Beasts
- Echarts关系图(使用重力图)
- Error:Execution failed for task ':app:clean'. Unable to delete directory: /media/file/workspaces/a
- 有关onpropertychange事件
- 热烈庆祝blog开通
- C语言的变量的作用域和生存期
- 深入理解Oracle Exadata
- Button.Text属性换行问题(Button.Bounds自动换行)
- JACK——PaintRobot Exercise6