c++ priority_queue应用(重要)
自定义排序
重写仿函数
struct cmp{
bool operator() ( Node a, Node b ){//默认是less函数
//返回true时,a的优先级低于b的优先级(a排在b的后面)
if( a.x== b.x ) return a.y> b.y;
return a.x> b.x; }
};
struct cmp1{
bool operator () ( int a , int b ){
return a > b;
}
};
struct cmp2{
bool operator ()( int s ,int d ){
return s<d;
}
};
priority_queue<int> q;//默认是从大到小。大顶堆
priority_queue<int, vector<int> ,less<int> >q;//从大到小排序。大顶堆
priority_queue<int, vector<int>, greater<int> >q;//从小到大排序。小顶堆
priority_queue < int , vector<int> , cmp2 > q;//从大到小。大顶堆
priority_queue < int , vector<int> , cmp1 > q;//从小到大。大顶堆
关于priority_queue中元素的比较
模板申明带3个参数:priority_queue<Type, Container, Functional>,其中Type 为数据类型,Container为保存数据的容器,Functional 为元素比较方式。
Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list。STL里面默认用的是vector。
如果把后面2个参数缺省的话,优先队列就是大顶堆(降序)
347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
Example 2:
Input: nums = [1], k = 1
Output: [1]
Note:
- You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
- Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
- It's guaranteed that the answer is unique, in other words the set of the top k frequent elements is unique.
- You can return the answer in any order
class Solution {
public:
struct cmp{
bool operator()(pair<int,int>& a,pair<int,int>& b){
if(a.second >= b.second) return true;
else return false;
}
};
vector<int> topKFrequent(vector<int>& nums, int k) {
//sort(nums.begin(),nums.end());
vector<int> res;
map<int,int> freq2num;
for(int i=0;i<nums.size();i++){
freq2num[nums[i]]++;
}
//默认大顶堆,我们需要小顶堆
priority_queue<pair<int,int>,vector<pair<int,int>>,cmp> p;
for(auto iter=freq2num.begin();iter!=freq2num.end();iter++){
if(p.size()<k){
p.push(*iter);
}else{
if(p.top().second<iter->second){
p.pop();
p.push(*iter);
}
}
}
while(p.size()){
res.push_back(p.top().first);
p.pop();
}
return res;
}
};
vector<int> topKFrequent(vector<int>& nums, int k) {
vector<int> res;
map<int,int> m;
for(auto c:nums)
{
m[c]++;
}
//默认大顶堆,我们选前k个。
priority_queue<pair<int,int>> q;
//map中iter->first是要返回的数字,ietr->second是数字的个数
for(auto iter=m.begin();iter!=m.end();iter++)
{
pair<int,int> pr=make_pair(iter->second,iter->first);
q.push(pr);
}
while(k--)
{
res.push_back(q.top().second);
q.pop();
}
return res;
}
c++ priority_queue应用(重要)的更多相关文章
- C++ std::priority_queue
std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...
- 【转载】STL之priority_queue
参考资料:传送门先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部.priority_queue特别之处在于,允许用户为队列中存储的元素 ...
- STL之priority_queue
下面以 long long 型队列介绍: Q.empty() // 判断队列是否为空 返回ture表示空 返回false表示空 bool Q.top() // 返回顶端元素的值 元素还在队列里 lon ...
- 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动
一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...
- STL之容器适配器priority_queue
priority_queue(优先队列)是一个拥有权值观念的queue,它允许加入新元素,删除旧元素,审视元素值等功能.由于这是一个queue,所以只允许在底端加入元素,并从顶端取出元素, 除此之外别 ...
- priority_queue 示例
http://www.cplusplus.com/reference/queue/priority_queue/ priority_queue 的top始终保持着为一堆数据中的最大元素. 读取最小 O ...
- 优先队列priority_queue的比较函数
STL头文件:#include<queue> 优先队列: 默认从大到小排列:priority_queuee<node>q; 自定义优先级的三种方法: 1.重载操作符: bool ...
- 5.1 stack,queue以及priority_queue
*:stack 使用要包含头文件stack,栈是一种先进后出的元素序列,删除和访问只能对栈顶的元素(最后一个添加的元素)进行,并且添加元素只能添加到栈顶.栈内的元素不能访问,要想访问先要删除其上方的所 ...
- hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏
huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...
- priority_queue 优先队列用法
//采用默认优先关系: //(priority_queue<int>que;) //Queue 0: // 91 83 72 56 47 36 22 14 10 7 3 // //采用结构 ...
随机推荐
- JSX 详解
一 jsx 的本质是什么? jsx是语法糖,需要被编译成js才能运行. jsx 看似是html 结构,实质是js结构的语法糖,在代码编译阶段被编译成js结构.所以jsx的本质可描述为看似html结构的 ...
- Mysql架构与内部模块-第一章
Mysql作为大多数中小型企业的首选数据库,也可能是众多同僚接触的第一个数据库,其热门程度不言而喻,一些相对基础的知识本系列不做赘述,主要简述Mysql相关的进阶知识. 本章将由浅入深的讲解从连接My ...
- linux(centos8):基于java13安装rocketmq-4.7.1(解决jdk不兼容的报错)
一,Rocketmq是什么? 1, RocketMQ是一个队列模型的消息中间件,具有高性能.高可靠.高实时.分布式特点 相比kafka,rocketmq的实时性更强 2,官方网站: http://ro ...
- linux(centos8):zabbix配置邮件报警(监控错误日志)(zabbix5.0)
一,zabbix5.0发邮件报警的准备工作: zabbix5.0在linux平台上的安装:参见这一篇: https://www.cnblogs.com/architectforest/p/129125 ...
- 老板,来几道web玩玩
好久没做web了,没想到还能自己做出来555 [MRCTF2020]Ez_bypass 签到题8 给了源码,一个md5强类型比较,然后post传参,弱类型判断,直接1234567a绕过了 I put ...
- C. Vladik and Memorable Trip 解析(思維、DP)
Codeforce 811 C. Vladik and Memorable Trip 解析(思維.DP) 今天我們來看看CF811C 題目連結 題目 給你一個數列,一個區段的數列的值是區段內所有相異數 ...
- 如何给office2019安装visio2019
1.关于版本,如果你的office2019推荐安装2019visio,2019office只有安装2019visio才不会互斥 下载地址是百度网盘传送,也要安装教程,可是里面的激活器用不了 https ...
- Azure Cosmos DB (四) 使用EF的SQL API 异地冗余
一,引言 上一篇文章中,我们介绍到使用了EF Core 与Cosmos DB SQL API 进行结合开发.同时,大家在开发过程中一定要记得EF Core 不支持Cosmos DB 的迁移.今天我们启 ...
- ORACLE 数据泵 expdp/impdp
ORACLE 数据泵 expdp/impdp 一.概念 Oracle Database 10g 引入了最新的数据泵(Data Dump)技术,数据泵导出导入 (EXPDP 和 IMPDP)的作用: 1 ...
- 【转】Extension Libraries and Loading Other Image Formats
FROM: http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/index. ...