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 ...
随机推荐
- PAT 1015 德才论
https://pintia.cn/problem-sets/994805260223102976/problems/994805307551629312 宋代史学家司马光在<资治通鉴>中 ...
- Python创建目录文件夹
Python对文件的操作还算是方便的,只需要包含os模块进来,使用相关函数即可实现目录的创建. 主要涉及到三个函数 1.os.path.exists(path) 判断一个目录是否存在 2.os.mak ...
- [C/C++] C++中new的语法规则
int *x = new int; //开辟一个存放整数的存储空间,返回一个指向该存储空间的地址(即指针) ); //开辟一个存放整数的空间,并指定该整数的初值为100,返回一个指向该存储空间的地址 ...
- 使用 TListView 控件(2)
本例效果图: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...
- 输入的是util包下面的 时间, 接受的是java.sql.date 或者 java.util.date类型
- 大步小步算法模板题, poj2417
大步小步模板 (hash稍微有一点麻烦, poj不支持C++11略坑) #include <iostream> #include <vector> #include <c ...
- Visual Studio调试之断点技巧篇补遗
原文链接地址:http://blog.csdn.net/Donjuan/article/details/4649372 讲完Visual Studio调试之断点技巧篇以后,翻翻以前看得一些资料和自己写 ...
- Adore 解题报告
Adore 问题描述 小\(\text{w}\) 偶然间得到了\(1\)个 \(DAG\). 这个 \(DAG\) 有 \(m\) 层,第\(1\)层只有\(1\)个源点,最后\(1\)层只有\(1\ ...
- 遇到问题---java---myeclipse中maven项目引用另一个导致的resource文件混乱的问题
遇到情况 情况是这样的,我们在构建项目时,经常会把一些公用的类和配置提取出去,作为一个公共项目.然后把公共项目作为一个jar包构件引入我们当前的项目中. 引入方式是 <dependency> ...
- 【CF edu 30 A. Chores】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...