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 // //采用结构 ...
随机推荐
- 【LWJGL3】LWJGL3的内存分配设计,第一篇,栈上分配
简介 LWJGL (Lightweight Java Game Library 3),是一个支持OpenGL,OpenAl,Opengl ES,Vulkan等的Java绑定库.<我的世界> ...
- localhost与127.0.0.1与0.0.0.0
localhost localhost其实是域名,一般系统默认将localhost指向127.0.0.1,但是localhost并不等于127.0.0.1,localhost指向的IP地址是可以配置的 ...
- Java 等待/通知机制
等待/通知的目的是确保等待线程从wait()方法返回时能够感知到通知线程对变量所做出的的修改: 等待方遵循如下原则: 1.获取对象的锁 2.如果条件不满足,那么调用对象的wait()方法,被通知后任要 ...
- 在学习python的过程中,遇到的最大的困难是什么?
本人文科生,回顾自己近 2 年的Python 自学经历,有一些学习心得和避坑经验分享给大家,让大家在学习 Python 的过程中少走一些弯路!减少遇到不必要的学习困难! 首先,最开始最大的困难应该就是 ...
- 503. 下一个更大元素 II
503. 下一个更大元素 II 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大 ...
- python3异步爬虫 ——aiohttp模板使用
一.简单使用和讲解 import aiohttp import asyncio async def fetch(client): async with client.get('http://httpb ...
- springboot添加拦截器
一,编写拦截器 public class TokenInterceptor implements HandlerInterceptor { @Override public boolean preHa ...
- Hadoop1.0 和 Hadoop2.0
date: 2018-11-16 18:54:37 updated: 2018-11-16 18:54:37 1.从Hadoop整体框架来说 1.1 Hadoop1.0即第一代Hadoop,由分布式存 ...
- Altium Designer中如何批量修改元器件封装?
我想你说的应该是altium里的封装管理库吧.1,Tools -> Footprint Manager -> ...2,在Component List里选择要改的器件3,在View and ...
- Triple的使用
public Triple<Long, Long, Double> getCarRunSummary(String did, Date startDate, Date endDate) { ...