让priority_queue支持小根堆的几种方法
点击这里了解什么是priority_queue
前言
priority_queue默认是大根堆,也就是大的元素会放在前面
例如

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
priority_queue<int>q;
int a[]={,,,,,};
const int n=;
int main()
{
for(int i=;i<=n;i++) q.push(a[i]);
while(q.size()!=)
printf("%d ",q.top()),q.pop();
return ;
}
它的输出结果是

那如何让priority_queue支持小根堆呢?:question:
方法一
将所有的数全部取负
这样的话绝对值小的数会变大,绝对值大的数会变小
这样就能实现小根堆了
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
priority_queue<int>q;
int a[]={,,,,,};
const int n=;
int main()
{
for(int i=;i<=n;i++) q.push(-a[i]);
while(q.size()!=)
printf("%d ",-q.top()),q.pop();
return ;
}

方法二
利用STL中自带的小根堆,很简单,只要在定义的时候写成
就好
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;//这样就可以实现小根堆了
int a[]={,,,,,};
const int n=;
int main()
{
for(int i=;i<=n;i++) q.push(a[i]);
while(q.size()!=)
printf("%d ",q.top()),q.pop();
return ;
}

另外
priority_queue是支持自定义比较函数的
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int n=;
struct node
{
int x,y;
node(){x=y=;}
node(int a,int b){x=a;y=b;}
};
priority_queue<node>q;
bool operator<(const node &a,const node &b)
{
if(a.x!=b.x)return a.x>b.x;
return a.y<b.y;
}
int a[]={,,,,,};
int b[]={,,,,,};
int main()
{
for(int i=;i<=n;i++) q.push(node(a[i],b[i]));
while(q.size()!=)
printf("%d %d\n",q.top().x,q.top().y),q.pop();
return ;
}

注意:priority_queue自定义函数的比较与sort正好是相反的,也就是说,如果你是把大于号作为第一关键字的比较方式,那么堆顶的元素就是第一关键字最小的
还可以这么写
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int n=;
struct node
{
int x,y;
node(){x=y=;}
node(int a,int b){x=a;y=b;}
bool operator <(const node &a)const
{
if(a.x!=x) return a.x<x;
return a.y>y;
}
};
priority_queue<node>q;
int a[]={,,,,,};
int b[]={,,,,,};
int main()
{
for(int i=;i<=n;i++) q.push(node(a[i],b[i]));
while(q.size()!=)
printf("%d %d\n",q.top().x,q.top().y),q.pop();
return ;
}
让priority_queue支持小根堆的几种方法的更多相关文章
- 让IE6IE7IE8支持CSS3属性的8种方法介绍
我们都知道,IE浏览器暂不支持CSS3的一些属性.国外的工程师们,不安于此现状,他们总是尽量使用一些手段使IE浏览器也能支持CSS3属性,我觉得这些都是很有意义,很有价值的工作,可以推动整个技术领域的 ...
- 让IE6/IE7/IE8支持CSS3属性的8种方法介绍
我们都知道,IE浏览器暂不支持CSS3的一些属性.国外的工程师们,不安于此现状,他们总是尽量使用一些手段使IE浏览器也能支持CSS3属性,我觉得这些都是很有意义,很有价值的工作,可以推动整个技术领域的 ...
- priority_queue()大根堆和小根堆(二叉堆)
#include<iostream> #include <queue> using namespace std; int main() { //对于基础类型 默认是大顶堆 pr ...
- 洛谷 P3378 【模板】堆(小根堆)
题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 输出该小根堆内的最小数 操作3: 3 删除该小根堆内的最小数 输入输出格式 输入格式: ...
- T-shirt buying CodeForces - 799B (小根堆+STL)
题目链接 思路: 由于题目说了只有1,2,3,三种色号的衣服,然后开三个对应色号的小根堆, 我是根据pair<int,int> 创建了一个以价格小的优先的优先队列. pair中的另外一个i ...
- CJOJ 2482 【POI2000】促销活动(STL优先队列,大根堆,小根堆)
CJOJ 2482 [POI2000]促销活动(STL优先队列,大根堆,小根堆) Description 促销活动遵守以下规则: 一个消费者 -- 想参加促销活动的消费者,在账单下记下他自己所付的费用 ...
- 【CF799B】T-shirt buying(一道很水的小根堆)
点此看题面 大致题意: 有\(n\)件T恤衫,告诉你每件T恤衫的价格以及它正面和反面的颜色(\(1≤\)颜色的编号\(≤3\)),现在有m个顾客,已知每个人想要的衣服的颜色(一件T恤衫只要有一面的颜色 ...
- 优先队列实现 大小根堆 解决top k 问题
摘于:http://my.oschina.net/leejun2005/blog/135085 目录:[ - ] 1.认识 PriorityQueue 2.应用:求 Top K 大/小 的元素 3 ...
- bzoj 1577: [Usaco2009 Feb]庙会捷运Fair Shuttle——小根堆+大根堆+贪心
Description 公交车一共经过N(1<=N<=20000)个站点,从站点1一直驶到站点N.K(1<=K<=50000)群奶牛希望搭乘这辆公交车.第i群牛一共有Mi(1& ...
随机推荐
- Dubbo工作原理,集群容错,负载均衡
Remoting:网络通信框架,实现了sync-over-async和request-response消息机制. RPC:一个远程过程调用的抽象,支持负载均衡.容灾和集群功能. Registry:服务 ...
- [Swift]LeetCode403. 青蛙过河 | Frog Jump
A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...
- [Swift]LeetCode530. 二叉搜索树的最小绝对差 | Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- [Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- Unable to preventDefault inside passive event listener due to target being treated as passive
Unable to preventDefault inside passive event listener due to target being treated as passive 今天在做项目 ...
- CentOS安装gcc clang git mysql等软件高版本福利
最近同事需要在单位提供的开发机上临时安装gcc等软件,时间紧迫,因此向其推荐安装福利devtoolset. 感谢devtoolset,使得CentOS软件安装无比的快捷,卸载也是无与伦比的简单. gc ...
- vue框架中的Axios封装
function axios(options) { let promise = new Promise((resolve, reject) => { var xhr ...
- DOM事件第二弹(UIEvent事件)
此文章主要总结UIEvent相关的事件,如有不对的地方,欢迎指正. 一.uitls.js(绑定事件公共类) var fixs = { 'focusin': { standard: 'focus', i ...
- 【Vue】IView之table组件化学习(二)
最基本的绑定table是这样的,需要columns和data两个属性. <template> <Card> <h4>表格栗子</h4> <Tabl ...
- 我对SQL性能优化的看法,对我的文章有提议的欢迎评论!
影响你的查询速度的原因是什么? 网速不给力,不稳定. 服务器内存不够,或者SQL 被分配的内存不够. sql语句设计不合理 没有相应的索引,索引不合理 表数据过大没有有效的分区设计 数据库设计太2,存 ...