自定义排序

重写仿函数

 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应用(重要)的更多相关文章

  1. C++ std::priority_queue

    std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...

  2. 【转载】STL之priority_queue

    参考资料:传送门先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部.priority_queue特别之处在于,允许用户为队列中存储的元素 ...

  3. STL之priority_queue

    下面以 long long 型队列介绍: Q.empty() // 判断队列是否为空 返回ture表示空 返回false表示空 bool Q.top() // 返回顶端元素的值 元素还在队列里 lon ...

  4. 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动

    一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...

  5. STL之容器适配器priority_queue

    priority_queue(优先队列)是一个拥有权值观念的queue,它允许加入新元素,删除旧元素,审视元素值等功能.由于这是一个queue,所以只允许在底端加入元素,并从顶端取出元素, 除此之外别 ...

  6. priority_queue 示例

    http://www.cplusplus.com/reference/queue/priority_queue/ priority_queue 的top始终保持着为一堆数据中的最大元素. 读取最小 O ...

  7. 优先队列priority_queue的比较函数

    STL头文件:#include<queue> 优先队列: 默认从大到小排列:priority_queuee<node>q; 自定义优先级的三种方法: 1.重载操作符: bool ...

  8. 5.1 stack,queue以及priority_queue

    *:stack 使用要包含头文件stack,栈是一种先进后出的元素序列,删除和访问只能对栈顶的元素(最后一个添加的元素)进行,并且添加元素只能添加到栈顶.栈内的元素不能访问,要想访问先要删除其上方的所 ...

  9. 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 ...

  10. priority_queue 优先队列用法

    //采用默认优先关系: //(priority_queue<int>que;) //Queue 0: // 91 83 72 56 47 36 22 14 10 7 3 // //采用结构 ...

随机推荐

  1. 多测师讲解接口测试 _postman(下)_高级讲师肖sir

    关联接口 定义:上个接口返回的参数作为下一个接口的入参 1)接口1:查询出所有的州,自治区,直辖市,省(且发送请求不需要入参) 接口url地址: http://www.webxml.com.cn/We ...

  2. 多测师讲解 ———python2和Python3区别

    python3.x和python2.x的区别:1.Python3.X源码文件默认使用utf-8编码,而python2.x的编译最前端需要加上#coding=utf-82.python3.x里打印pri ...

  3. C语言从1打印到100再打印到1该如何编写?我只服最后一种写法!

    我觉得这是一个送分题,奈何人才太多了,给出了各种古怪的写法,如果是做项目的话,我比骄建议一些正常的写法,就是大家都能看得懂的,不要搞什么花里胡哨,不过你要是交流的话,既然是交流,我不觉得要多正规,即使 ...

  4. 【最短路】HDU 1688 Sightseeing

    题目大意 给出一个有向图(可能存在重边),求从\(S\)到\(F\)最短路的条数,如果次短路的长度仅比最短路的长度多1,那么再加上次短路的条数. 输入格式 第一行是数据组数\(T\). 对于魅族数据, ...

  5. java安全编码指南之:Thread API调用规则

    目录 简介 start一个Thread 不要使用ThreadGroup 不要使用stop()方法 wait 和 await 需要放在循环中调用 简介 java中多线程的开发中少不了使用Thread,我 ...

  6. oozie.action.hadoop.LauncherException: IO error Connection timed out: no further information

    本文主要针对使用CDH平台的HUE时候碰到两类问题,最终问题并没有得到很好的解决,只是提供了一种绕行方式,欢迎知道的朋友补充. ## **NO 1: HUE执行jar包** > 第一种报错 or ...

  7. arcgis activeX 安全提示消除办法

    点击任何的一个ArcToolBox 工具,光标落在参数输入框时,会提示 "在此页面上的ActiveX控件和本页上的其他部分的交互可能不安全.你想允许这种交互操作吗? " 消除办法 ...

  8. CentOS8 安装

    CentOS8 1911 下载 https://mirrors.aliyun.com/centos/8/isos/x86_64/CentOS-8.1.1911-x86_64-dvd1.iso Step ...

  9. java安全编码指南之:线程安全规则

    目录 简介 注意线程安全方法的重写 构造函数中this的溢出 不要在类初始化的时候使用后台线程 简介 如果我们在多线程中引入了共享变量,那么我们就需要考虑一下多线程下线程安全的问题了.那么我们在编写代 ...

  10. maven中pom.xml文件配置

    <properties>                <spring.version>4.3.18.RELEASE</spring.version>        ...