点击这里了解什么是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支持小根堆的几种方法的更多相关文章

  1. 让IE6IE7IE8支持CSS3属性的8种方法介绍

    我们都知道,IE浏览器暂不支持CSS3的一些属性.国外的工程师们,不安于此现状,他们总是尽量使用一些手段使IE浏览器也能支持CSS3属性,我觉得这些都是很有意义,很有价值的工作,可以推动整个技术领域的 ...

  2. 让IE6/IE7/IE8支持CSS3属性的8种方法介绍

    我们都知道,IE浏览器暂不支持CSS3的一些属性.国外的工程师们,不安于此现状,他们总是尽量使用一些手段使IE浏览器也能支持CSS3属性,我觉得这些都是很有意义,很有价值的工作,可以推动整个技术领域的 ...

  3. priority_queue()大根堆和小根堆(二叉堆)

    #include<iostream> #include <queue> using namespace std; int main() { //对于基础类型 默认是大顶堆 pr ...

  4. 洛谷 P3378 【模板】堆(小根堆)

    题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 输出该小根堆内的最小数 操作3: 3 删除该小根堆内的最小数 输入输出格式 输入格式: ...

  5. T-shirt buying CodeForces - 799B (小根堆+STL)

    题目链接 思路: 由于题目说了只有1,2,3,三种色号的衣服,然后开三个对应色号的小根堆, 我是根据pair<int,int> 创建了一个以价格小的优先的优先队列. pair中的另外一个i ...

  6. CJOJ 2482 【POI2000】促销活动(STL优先队列,大根堆,小根堆)

    CJOJ 2482 [POI2000]促销活动(STL优先队列,大根堆,小根堆) Description 促销活动遵守以下规则: 一个消费者 -- 想参加促销活动的消费者,在账单下记下他自己所付的费用 ...

  7. 【CF799B】T-shirt buying(一道很水的小根堆)

    点此看题面 大致题意: 有\(n\)件T恤衫,告诉你每件T恤衫的价格以及它正面和反面的颜色(\(1≤\)颜色的编号\(≤3\)),现在有m个顾客,已知每个人想要的衣服的颜色(一件T恤衫只要有一面的颜色 ...

  8. 优先队列实现 大小根堆 解决top k 问题

      摘于:http://my.oschina.net/leejun2005/blog/135085 目录:[ - ] 1.认识 PriorityQueue 2.应用:求 Top K 大/小 的元素 3 ...

  9. bzoj 1577: [Usaco2009 Feb]庙会捷运Fair Shuttle——小根堆+大根堆+贪心

    Description 公交车一共经过N(1<=N<=20000)个站点,从站点1一直驶到站点N.K(1<=K<=50000)群奶牛希望搭乘这辆公交车.第i群牛一共有Mi(1& ...

随机推荐

  1. [运维工具]linux下远程桌面rdesktop安装和使用

    依然是解压 configure make make install 这些步骤 rdesktop -f 16 192.168.16.90 -f是全屏,退出全屏是CRTL+ALT+ENTER 记录一个li ...

  2. CESSNA: Resilient Edge-Computing

    CESSNA: 弹性边缘计算 本文为SIGCOMM 2018 Workshop (Mobile Edge Communications, MECOMM)论文. 笔者翻译了该论文.由于时间仓促,且笔者英 ...

  3. IOS - 上APPSTORE为何因IPv6被拒?

    http://blog.csdn.net/wanglixin1999/article/details/52182001

  4. MySQL数据备份方法

    MySQL的备份和还原 备份:副本    RAID1,RAID10:保证硬件损坏而不会业务中止:        DROP TABLE mydb.tb1; 备份类型:        热备份.温备份和冷备 ...

  5. [Swift]LeetCode406. 根据身高重建队列 | Queue Reconstruction by Height

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...

  6. [Swift]LeetCode768. 最多能完成排序的块 II | Max Chunks To Make Sorted II

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

  7. 第四周 IP通信基础回顾

    传输层的作用:分割上层数据:在应用主机程序之间建立到端的连接:流量控制:面向连接与面向非连接. URG=1,紧急指针字段有效. 窗口字段- 占2个字节,用来让对方设置发送窗口的依据,单位为字节. TC ...

  8. BBS论坛(五)

    5.1.cms后台修改密码功能完成 (1)新建app/forms.py # app/forms.py from wtforms import Form class BaseForm(Form): de ...

  9. 【Zara原创】SqlSugar4轻量级ORM框架的使用指南

    前言:sqlSugar出生已经有3年之久了,从1.0到现在的4.x的版本,为了以后方便使用SqlSugar,所以特意花了2个小时来叙述它. 关于SqlSugar 性能:性能最好的ORM之一,具有超越D ...

  10. Python爬虫入门教程 28-100 虎嗅网文章数据抓取 pyspider

    1. 虎嗅网文章数据----写在前面 今天继续使用pyspider爬取数据,很不幸,虎嗅资讯网被我选中了,网址为 https://www.huxiu.com/ 爬的就是它的资讯频道,本文章仅供学习交流 ...