优先队列
来源:互联网 发布:让我拥抱你倔强知乎 编辑:程序博客网 时间:2024/05/16 01:47
#include<stdio.h>#include<iostream>#include<algorithm>#include<cstring>#include<queue>using namespace std;struct cmp1{ bool operator ()(int &a,int &b) { return a>b;//最小值优先 }};struct cmp2{ bool operator ()(int &a,int &b) { return a<b;//最大值优先 }};int main(){ priority_queue<int,vector<int>,cmp1> que1;//自定义 小指有限 priority_queue<int,vector<int>,cmp2> que2;//自定义 大致有限 priority_queue<int,vector<int>,greater<int> > que3;//小指优先 。 priority_queue<int,vector<int>,less<int> > que4;//大只优先。 int n,a; scanf("%d",&n); for(int i=1;i<=n;i++) { cin>>a; que1.push(a); que2.push(a); que3.push(a); que4.push(a); } while(!que1.empty()) { cout<<que1.top()<<endl; que1.pop(); } cout<<endl; while(!que2.empty()) { cout<<que2.top()<<endl; que2.pop(); } cout<<endl; while(!que3.empty()) { cout<<que3.top()<<endl; que3.pop(); } cout<<endl; while(!que4.empty()) { cout<<que4.top()<<endl; que4.pop(); } cout<<endl; return 0;}
在结构体里直接定义
#include<stdio.h>#include<iostream>#include<algorithm>#include<cstring>#include<queue>using namespace std;struct cmp1{ bool operator ()(int &a,int &b) { return a>b;//最小值优先 }};struct cmp2{ bool operator ()(int &a,int &b) { return a<b;//最大值优先 }};struct node{ int p; friend bool operator <(node x,node y) { return x.p>y.p; }} a[500005];struct node1{ int p; friend bool operator <(node1 x,node1 y) { return x.p<y.p; }} b[500005];int main(){ priority_queue<int,vector<int>,cmp1> que1;//自定义 小指有限 priority_queue<int,vector<int>,cmp2> que2;//自定义 大致有限 priority_queue<int,vector<int>,greater<int> > que3;//小指优先 。 priority_queue<int,vector<int>,less<int> > que4;//大只优先。 priority_queue<node>q; priority_queue<node1>q1; int n; scanf("%d",&n); for(int i=1;i<=n;i++) { // cin>>a[i].p; cin>>b[i].p; q1.push(b[i]); // q.push(a[i]); } while(!q1.empty()) { cout<<q1.top().p; q1.pop(); } cout<<endl; return 0;}
0 0
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 优先队列
- 让CoreData更简单些
- 第三方很好的Toast
- 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)
- 06-S3C2440学习之移植2012u-boot到S3C2440(移植过程四)支持Nand Flash+支持DM9000网卡
- Leetcode -- 28. Implement strStr()
- 优先队列
- redux 三重境
- [随便搞搞] TCO14 Round 2C InverseRMQ
- 平衡二叉树
- 改进的冒泡排序
- LayoutAnimation为ViewGroup子View添加出现特效
- [BZOJ2004]公交线路 DP+矩阵快速幂
- 【敏捷开发每日一贴】测试驱动开发
- jQuery的deferred对象详解