优先队列

算法(第4版)-2.4 优先队列

本小妞迷上赌 提交于 2020-02-24 16:26:42
定义:一种支持删除最大元素和插入元素的数据结构。 经典实现:基于二叉堆数据结构。 2.4.1 API 1. 只要我们能够高效地实现insert()和delMin(),下面的优先队列用例中调用了MinPQ的TopM就能使用优先队列解决这个问题。 2.4.2 初级实现 1. 数组实现(无序): 修改pop(),先交换再删除,相当于选择排序(个人认为)。 -> 惰性方法 2. 数组实现(有序): 修改insert(),每次插入后保证最大值在栈的顶部。 -> 积极方法 3. 链表表示法: 用基于链表的下压栈的代码作为基础,选择上面二者之一实现。 4. 优先队列的各种实现在最坏情况下运行时间的增长数量级 数据结构 插入元素 删除最大元素 有序数组 N 1 无序数组 1 N 堆 logN logN 理想情况 1 1 2.4.3 堆的定义 1. 堆有序:一棵二叉树的每个结点都大于等于它的两个子结点。 2. 根节点是堆有序的二叉树中的最大结点。 3. 二叉堆是一组能够用堆有序的完全二叉树排序的元素,并在数组中按照层级储存(不使用数组的第一个位置)。 4.* 在一个堆中,位置k的结点的父结点的位置为⌊k / 2⌋,而它的两个子结点的位置则分别为2k和2k + 1。 5. 一棵大小为N的完全二叉树的高度为⌊lgN⌋。 2.4.4 堆的算法 1. 理解上浮(swim)和下沉(sink)。 2. 插入元素

优先队列

风格不统一 提交于 2020-02-24 15:50:54
许多应用程序都需要处理有序的元素,但不一定要求全部有序。一个合适的数据结构应该支持两种操作:删除最大元素和插入元素。 1 #include <iostream> 2 #include <queue> 3 using namespace std; 4 5 6 struct node 7 { 8 int priority; 9 int value; 10 friend bool operator<(node n1, node n2) 11 { 12 return n1.priority < n2.priority; 13 } 14 }; 15 16 17 struct cmp 18 { 19 bool operator() (const int &a, const int &b) 20 { 21 return a > b; 22 } 23 }; 24 25 int main() 26 { 27 // Use 1 28 cout << "Use 1" << endl; 29 priority_queue<int> qi; 30 qi.push(10); 31 qi.push(78); 32 qi.push(13); 33 qi.push(12); 34 qi.push(50); 35 36 // expect output is 78 50 13 12 10 37 while(!qi

优先队列+结构体 设置优先级

偶尔善良 提交于 2020-02-20 02:08:17
今天写了几个优先队列的题目 涉及到设置优先级 就去现学了一点 我还不是很熟悉 就先记录下基础模板吧 等学会了重载运算符再细细总结 #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<queue> #include<vector> #include<map> #include<stack> #include<algorithm> #define ll long long using namespace std; const int N=6e4+100; struct node { int val; int num; friend bool operator <(const node &a,const node &b) {///记得这个比较和逻辑是相反的 if(a.val==b.val) return a.num>b.num; else return a.val<b.val; } }; int main() { int n; while(~scanf("%d",&n)) { priority_queue<node>pq[4]; char ope[10]; int id; int cnt=0; for(int i=1;i<=n;i++) { getchar(); scanf("%s

B - 优先队列 POJ - 2970

孤者浪人 提交于 2020-02-17 15:26:53
1. 题目 POJ - 2970 A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is a web-designer and an executive director at the same time. The second one is a programmer. The director is so a nimble guy that the studio has already got N contracts for web site development. Each contract has a deadline di. It is known that the programmer is lazy. Usually he does not work as fast as he could. Therefore, under normal conditions the programmer needs bi of time to perform the contract number i. Fortunately, the guy is very greedy for money. If the director pays

LeetCode 23 Hard,K个链表归并

一世执手 提交于 2020-02-17 09:04:02
本文始发于个人公众号: TechFlow ,原创不易,求个关注 链接 Merge k Sorted Lists 难度 Hard 描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 给定K个有序链表,要求将它所有的元素归并到一个链表,并且有序。 样例: Input: [ 1->4->5, 1->3->4, 2->6 ] Output: 1- >1->2->3->4->4->5->6 题解 按照惯例,我们还是先来看最简单的解法,也就是暴力法。 暴力 这题当中,暴力的方法也很简单,非常简单粗暴,那就是先把所有元素取出来,排序之后再放到链表当中去。但是这么做说实话有点脱裤子放屁,我们分析一下复杂度也会发现,假设所有的元素个数是n,那么最后的复杂度应该就是排序所消耗的复杂度,也就是 \(O(nlogn)\) ,和K没有一点关系,而且我们也完全没有用上这K个链表当中的元素都是有序的这个信息,显然这是不科学的。 我们对上面的纯暴力方法稍稍做一些优化,想办法把K个链表当中元素有序的信息用上。用上的方法也很简单,我们之前做归并排序的时候曾经做过两个数组的归并,我们用同样的方法,只不过我们这次换成是K个链表而已。也就是说我们每次遍历这K个链表的头元素

优先队列总结:知识点加题目

倾然丶 夕夏残阳落幕 提交于 2020-02-14 01:55:47
优先队列的定义:优先级高的在前面,优先级低的在后面,每次操作后会自动调整! 1 .priority_queue<int,vector,less >q;//从大到小 2 .priority_queue<int,vector,greater >q;//从小到大 3 .priority_queueq; struct node { int x,y; long long sum; }; bool operator < (const node &s1,const node &s2) { return s1.sum>s2.sum;//是从小到大排列的 } 优先队列常见操作: 1 .q.pop(); 2 .q.top(); 3 .q.push(x); 优先队列题目: 1. http://acm.nefu.edu.cn/problemShow.php?problem_id=1537 在这里插入代码片优先队列中放结构体,自定义比较结构体需要重载运算符,功能和sort中的自定义比较函数cmp一样。 # include <bits/stdc++.h> using namespace std ; int n , x ; struct node { int x , num ; } ; bool operator < ( const node & s1 , const node & s2 ) /

NEFU 优先队列

匆匆过客 提交于 2020-02-14 00:44:29
大一寒假集训八 优先队列 合并果子-优先队列 # include <bits/stdc++.h> using namespace std ; priority_queue < int , vector < int > , greater < int > > vis ; //从小到大排列,固定格式记住就好 int main ( ) { int n ; cin >> n ; for ( int i = 0 ; i < n ; i ++ ) { int x ; cin >> x ; vis . push ( x ) ; } int res = 0 ; while ( vis . size ( ) >= 2 ) { int a = vis . top ( ) ; //优先队列中取出队首元素用top,区别队列中用front vis . pop ( ) ; int b = vis . top ( ) ; vis . pop ( ) ; vis . push ( a + b ) ; res = res + a + b ; } cout << res << endl ; return 0 ; } 合成陨石-优先队列 # include <bits/stdc++.h> using namespace std ; priority_queue < int , vector < int > ,

HDU 1285 拓扑排序+优先队列

。_饼干妹妹 提交于 2020-02-12 02:27:47
传送门HDU1285 拓扑排序+优先队列 注意坑点:输入时需要判断重边,否则入度会出错 # include <bits/stdc++.h> using namespace std ; priority_queue < int , vector < int > , greater < int > > q ; //升序优先排列 //priority_queue<int, vector<int >, less<int > >q 为降序排列 //priority<int> q 默认为降序排列 字典序大的为q.top() const int maxn = 510 ; bool G [ maxn ] [ maxn ] ; int in [ maxn ] ; int n , m ; void toposort ( ) { for ( int i = 1 ; i <= n ; i ++ ) if ( in [ i ] == 0 ) q . push ( i ) ; //入度为零压入队列 int c = 1 ; //控制输出格式 while ( ! q . empty ( ) ) { int v = q . top ( ) ; q . pop ( ) ; //取出队列第一个元素并弹出 if ( c != n ) { printf ( "%d " , v ) ; c ++ ; } else

进程优先队列

爱⌒轻易说出口 提交于 2020-02-09 11:37:18
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StackQueue.algo { //进程优先队列 public struct pqItem { //通常会把存储在优先队列中的数据项作为键值对来构造,其中关键字就是指优先级别,而值则用来识别数据项。 //例如,可以按照如下形式对一个操作系统进程进行定义: public int priority; public string name; }//public struct pqItem public class PQueue : Queue { //大家不能把未修改的 Queue 对象用于优先队列。 DeQueue 方法在被调用时只会把队列中的第一个数据项移除。 //但是,大家可以从 Queue 类派生出自己的优先队列类,同时覆盖 DeQueue 方法来实现自己的需求。 //大家把这个类称为 PQueue。所有 Queue 的方法都可以照常使用,同时覆盖 Dequeue 方法来移除具有最高优先 //级的数据项。为了不从队列前端移除数据项,首先需要把队列的数据项写入一个数组

优先队列,sort(搬运工)

Deadly 提交于 2020-02-07 02:33:22
priority_queue < Type , Container , Functional > 优先队列 1. https : / / www . cnblogs . com / huashanqingzhu / p / 11040390. html //优先队列具有队列的所有特性,包括队列的基本操作,只是在这基础上添加了内部的一个排序,它本质是一个堆实现的。 //对于基础类型 默认是大顶堆 priority_queue < int > a ; //等同于 priority_queue<int, vector<int>, less<int> > a; // 这里一定要有空格,不然成了右移运算符>> ↓↓ priority_queue < int , vector < int > , greater < int > > c ; //这样就是小顶堆 priority_queue < string > b ; 2. https : / / www . cnblogs . com / yaoyueduzhen / p / 4456430. html top ( ) 返回优先队列对顶元素 //队列中最大的元素总是位于队首,所以出队时,并非按照先进先出的原则进行,而是将当前队列中最大的元素出队 //元素的比较规则默认按元素值由大到小排序,可以重载“<”操作符来重新定义比较规则。