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 // //采用结构 ...
随机推荐
- Unix中使用MeteoInfo - Xmanager设置
通过Xshell等客户端登陆Unix系统运行图形软件(X11)需要有X-server,Xmanager是其中的佼佼者(可惜是商业软件).我通常用Xshell登陆气象局的IBM高性能计算机(AIX系统) ...
- IDEA项目路径初探
IDEA项目路径 普通Java项目 普通Java项目,标准目录结构src下的路径就是classpath类路径,每次编译都会将src目录下新增的类和资源文件打包进类路径. 如下图,类文件和配置文件都会被 ...
- python实现elasticsearch操作-CRUD API
python操作elasticsearch常用API 目录 目录 python操作elasticsearch常用API1.基础2.常见增删改操作创建更新删除3.查询操作查询拓展类实现es的CRUD操作 ...
- spring boot:使用mybatis访问多个mysql数据源/查看Hikari连接池的统计信息(spring boot 2.3.1)
一,为什么要访问多个mysql数据源? 实际的生产环境中,我们的数据并不会总放在一个数据库, 例如:业务数据库:存放了用户/商品/订单 统计数据库:按年.月.日的针对用户.商品.订单的统计表 因为统计 ...
- linux(centos8):prometheus使用mtail监控错误日志
一,mtail的用途? mtail :从应用程序日志中提取指标以导出到时间序列数据库或时间序列计算器 它是一个google开发的日志提取工具,用途就是: 实时读取应用程序的日志. 再通过自己编写的脚本 ...
- ansible使用playbook的简单例子(ansible2.9.7)
一,ansible使用playbook的优点 1,用ansible执行一些简单的任务,使用ad-hoc命令就可以解决问题 如果执行复杂的功能,需要大量的操作,执行的ad-hoc命令会不够方便,这时我们 ...
- jquery1.9+,jquery1.10+ 为什么不支持live方法了?
live() 替换成 on() die() 替换成off() 根据jQuery的官方描述,live方法在1.7中已经不建议使用,在1.9中删除了这个方法.并建议在以后的代码中使用on方法来替代. o ...
- Python初识和变量基础
Python是面向对象,动态解释型和强类型的语言 编译型: 将代码一次性全部编译成二进制,然后再执行 优点:执行效率高. 缺点:开发效率低. 代表语言:C 解释型: 逐行解释成二进制,逐行运行 优点: ...
- FrameworkElementFactory中的SetBinding与SetValue
public static Microsoft.Windows.Controls.DataGridColumn CreateDateColumn(string path, string header) ...
- Topsis优劣解距离法 mlx代码
请参考https://blog.csdn.net/qq_36384657/article/details/98188769 mlx代码 topsis 优劣解距离法 参数说明: 分数.获奖次数.价值等 ...