Max-heap && Min-heap && push_heap
最大堆:make_heap(vi.begin(),vi.end())
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int ia[] = {, , , , , , , , };
make_heap(ia, ia + );
for(int i = ; i < ; i++)
{
cout << ia[i] << " ";
}
cout << endl;
make_heap(ia, ia + );
pop_heap(ia, ia + );
cout << ia[] << endl;
}
最小堆:sort_heap(vi,vi+n)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int ia[] = {, , , , , , , , };
sort_heap(ia, ia + );
for(int i = ; i < ; i++)
{
cout << ia[i] << " ";
}
cout << endl;
make_heap(ia, ia + );
pop_heap(ia, ia + );
cout << ia[] << endl;
}
Max-heap 取走根节点后完全二叉树的排序:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int ia[] = {, , , , , , , , };
vector<int>vi(ia, ia + );
make_heap(vi.begin(), vi.end());
for(int i = ; i < vi.size(); i++)
{
cout << vi[i] << " ";
}
cout << endl;
pop_heap(vi.begin(), vi.end());
for(int i = ; i < vi.size(); ++i)
{
/* code */
cout << vi[i] << " ";
}
cout << endl;
for(int i = ; i < vi.size(); ++i)
{
/* code */
cout << vi[i] << " ";
}
cout << endl;
return ;
}
Max-heap && Min-heap && push_heap的更多相关文章
- implement min heap
class MinHeap{ private ArrayList<Integer> arr; private int DEFAULT_LEN = 10; public MinHeap(){ ...
- find K maximum value from an unsorted array(implement min heap)
Maintain a min-heap with size = k, to collect the result. //Find K minimum values from an unsorted a ...
- JAVA Shallow heap & Retained heap
最近在研究内存泄漏的问题,在使用MAT工具中发现了Shallow heap & Retained heap,不懂. 然后在网上找了一些资料. Shallow Size 对象自身占用的内存大小, ...
- java.lang.OutOfMemoryError: PermGen space PermGen space & java.lang.OutOfMemoryError: Java heap space Heap siz
java.lang.OutOfMemoryError: PermGen space PermGen space 由-XX:PermSize -XX:MaxPermSize 引起 java.lang. ...
- 集合函数AVG,SUM,MAX,MIN
需要计算这些值的平均值.使用函数AVG(),你可以返回一个字段中所有值的平均值. 假如你对你的站点进行一次较为复杂的民意调查.访问者可以在1到10之间投票,表示他们喜欢你站点的程度.你把投票结果保存在 ...
- 【转载】set_input_delay和set_output_delay的选项-max和-min的讨论
转自:http://www.cnblogs.com/freshair_cnblog/archive/2012/09/12/2681060.html 一.存在背景分析 文档的说法是,set_input_ ...
- Oracle bug 使用max或min函数into到一个char类型报字符缓冲区太小的错误
这个BUG出现会报错如下: selectto_char(max(RENEWAL_DATE)) intoM_YEAR_MONTH fromt_renewal_schedule; ORA-06502: P ...
- mvc 中Range中max和min值晚绑定
对于Attribute : Range(min,max)的min和max必须在用的时候给,但是需求有时须要把这两个值存db.动态取出的.这时就须要razor帮忙了: @Html.TextBoxFor( ...
- SQL使用总结-like,MAX,MIN
1. 时间索引不容许使用like 对时间索引适应like,会时间索引变成字符串操作,成为遍历动作,失去索引价值. 错误写法: EXPLAIN SELECT AVG(data_value) AS av ...
- zip()函数,max()和min(),built-in function,import模块,read(),readlines(),write(),writelines(),with..as..文件处理方式
zip()函数:将可迭代对象作为参数,将对象中的对应元素打包成一个个元组. #map()普通的输出例子 print(list(zip(('a','n','c'),(1,2,3)))) print(li ...
随机推荐
- HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)
Description P. T. Tigris is a student currently studying graph theory. One day, when he was studying ...
- LCA(最近公共祖先)——离线 Tarjan 算法
tarjan算法的步骤是(当dfs到节点u时):1 在并查集中建立仅有u的集合,设置该集合的祖先为u1 对u的每个孩子v: 1.1 tarjan之 1.2 合并v到父节点u的集合,确保集合的祖 ...
- DFS——hdu1016Prime Ring Problem
一.题目回顾 题目链接:Prime Ring Problem Problem Description A ring is compose of n circles as shown in diagra ...
- beta版本冲刺三
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- 有关parent.frame.cols在firefox浏览器上不兼容的问题解决
IE(不兼容FireFox): if(parent.myFrame.cols == "199,7,*") { parent.myFrame.cols="0,7,*&quo ...
- Struts1之bean标签
用于输出 <%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %> ...
- hdu DIY FLIGHT GAME (dfs)
FLIGHT GAME Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total S ...
- sql如何先排序再去重
场景 有一张得分表(score),记录了用户每次的得分,同一个人可能有多个得分. id name score 1 tom 45 2 jack 78 3 tom 34 . . . 需求:找出分数最高的前 ...
- [洛谷P3254]圆桌问题
题目大意:有$m$个单位,每个单位有$r_i$个代表,有$n$张餐桌,每张餐桌可容纳$c_i$个代表.要求同一个单位的代表不在同一个餐桌就餐.若可以,输出$1$以及其中一种方案,否则输出$0$ 题解: ...
- [Leetcode] distinct subsequences 不同子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...