让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& ...
随机推荐
- 5 个免费的受欢迎的 SQLite 管理工具【申明:来源于网络】
5 个免费的受欢迎的 SQLite 管理工具 包含内容: SQLite Expert – Personal Edition SQLite Expert 提供两个版本,分别是个人版和专业版.其中个人版是 ...
- IIS集中化管理与编程REST API
目录 一. 简介.... 1 二. 安装.... 1 三. IIS Administration配置文件.... 2 四. Web UI管理界面.... ...
- [Swift]LeetCode693. 交替位二进制数 | Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- [Swift]LeetCode1024. 视频拼接 | Video Stitching
You are given a series of video clips from a sporting event that lasted T seconds. These video clip ...
- Docker系列教程02-MongoDB默认开启鉴权
说明,我这里使用的是compose的版本的1.17.0格式是3,但是这和compose版本无关,你只需要添加MONGO_INITDB_ROOT_USERNAME和MONGO_INITDB_ROOT_P ...
- 网络协议 8 - TCP协议(上):性恶就要套路深
系列文章: 网络协议 1 - 概述 网络协议 2 - IP 是怎么来,又是怎么没的? 网络协议 3 - 从物理层到 MAC 层 网络协议 4 - 交换机与 VLAN:办公室太复杂,我要回学校 网络协议 ...
- 记Javascript一道题的理解
代码如下: function Foo(){ getName = function(){ console.log("1"); } return this; } Foo.getName ...
- phpStrom映射代码
- Android--从系统Gallery获取图片
前言 在Android应用中,经常有场景会需要使用到设备上存储的图片,而直接从路径中获取无疑是非常不便利的.所以一般推荐调用系统的Gallery应用,选择图片,然后使用它.本篇博客将讲解如何在Andr ...
- Linux学习笔记(一):常用命令(1)
经过统计Linux中能够识别的命令超过3000种,当然常用的命令就远远没有这么多了,按照我的习惯,我把已经学过的Linux常用命令做了以下几个方面的分割: 1.文件处理命令 2.文件搜索命令 3.帮助 ...