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 // //采用结构 ...
随机推荐
- CSS的元素显示模式与转换
CSS的元素显示模式与转换 1. CSS的元素显示模式 1.1 块元素 <div>标签是最典型的块元素.另外常见的块元素有h1~h6.p.ul.ol.li等. 特点: 独占一行 高度.宽度 ...
- 记录一次源码扩展案列——FastJson自定义反序列化ValueMutator
背景:曾经遇到一个很麻烦的事情,就是一个json串中有很多占位符,需要替换成特定文案.如果将json转换成对象后,在一个一个属性去转换的话就出出现很多冗余代码,不美观也不是很实用. 而且也不能提前在j ...
- Android开发教程之密码框右侧显示小眼睛
现在都说互联网寒冬,其实只要自身技术能力够强,咱们就不怕!我这边专门针对Android开发工程师整理了一套[Android进阶学习视频].[全套Android面试秘籍].[Android知识点PDF] ...
- day33 Pyhton 常用模块03
一.正则表达式: 1.元字符 . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线 \s 匹配任意的空白符 \d 匹配数字 \n 匹配一个换行符 \t 匹配一个制表符 \b 匹配一个单词的结尾 ...
- 对json数组按照id精确查询并修改值
//json数组,里面有一个id等于5的,班级的标识和名称不是该班级,通过id把班级信息修改为指定的信息 var zNodes=[ { id:1, classid:1, className:" ...
- PHP获取当前毫秒级别时间戳
PHP提供了一个microtime()函数,调用时不带可选参数,本函数以"msec sec"的格式返回一个字符串,其中 sec 是自 Unix 纪元(0:00:00 January ...
- Pyqy5 让窗口居中
# QDesktopWidget import sys from PyQt5.QtWidgets import QDesktopWidget,QMainWindow,QApplication from ...
- 校招“避雷针”——GitHub 热点速览 Vol.43
作者:HelloGitHub-小鱼干 如果要选一个关键词来概述本周的 GitHub Trending,保护 便是不二之选.先是有 ShameCom 来为应届毕业生护航,让学弟学妹们不被黑名单上的公司上 ...
- ORB-SLAM3 Initializer.cpp函数解读
作者: Liam 点击上方"计算机视觉工坊",选择"星标" 干货第一时间送达 构造函数 Initializer::Initializer(const Frame ...
- Java学习的第四十三天
1.例5.1数组元素的引用 public class cjava { public static void main(String[] args) { int i; int []a=new int[1 ...