点击这里了解什么是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. mac本webstrom破解

    之前忙着加班一直没搞,有时间解决一下 首先编辑hosts文件 https://jingyan.baidu.com/article/f3ad7d0f55154309c3345bdd.html Mac系统 ...

  2. High Availability手册(1): 环境

    三台KVM虚拟机 首先我们得有一个pacemaker的环境,需要三台机器,如果没有那么多物理机器,可以用kvm虚拟机 创建一个bridge ovs-vsctl add-br ubuntu_br ifc ...

  3. 动态规划----最长公共子序列(LCS)问题

    题目: 求解两个字符串的最长公共子序列.如 AB34C 和 A1BC2   则最长公共子序列为 ABC. 思路分析:可以用dfs深搜,这里使用到了前面没有见到过的双重循环递归.也可以使用动态规划,在建 ...

  4. [Swift]LeetCode77. 组合 | Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

  5. [Swift]LeetCode838. 推多米诺 | Push Dominoes

    There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...

  6. [Swift]LeetCode1014. 最佳观光组合 | Best Sightseeing Pair

    Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and t ...

  7. 接口平台经常报server internal error(500)错误

    查询日志,发现连接mysql报错,web页面显示server internal error(500) 解决方法:重启mysql服务器 systemctl start mysqld #安装mysql # ...

  8. Java中需要知道的关键字

    Java中有一些或常用,或不常用,但却不得不知关键字,本篇文章将讨论这些关键字的作用. transient transient关键字可能用的不是那么频繁,但却是一个很重要的关键字,它的作用是在对象序列 ...

  9. 微信小程序入门(五)

    24.MINA框架讲解 MINA框架架构 25.小程序运行机制 小程序在首次打开的时间会比较长,后续再打开启动会很快,那么小程序是如何启动的呢? 运行机制-启动 冷启动 热启动 热启动:假入用户已经打 ...

  10. 前端(各种demo)一:css实现三角形,css实现梯形,pop弹层,css伪类before,after使用,svg使用(持续更新中)

    各种demo: 1.css实现正方形 思路:width为0:height为0:使用boder-width为正方形的边长的一半,不占任何字节:border-style为固体:border-color为正 ...