最大堆、最小堆

定义:

最大堆:根结点的键值是所有堆结点键值中最大者的堆。
最小堆:根结点的键值是所有堆结点键值中最小者的堆。


维基百科链接



C++中的STL里边定义的堆操作:

STL里面的堆操作一般用到的只有4个:make_heap();、pop_heap();、push_heap();、sort_heap();
他们的头文件函数是#include <algorithm>

首先是make_heap();
他的函数原型是:void make_heap(first_pointer,end_pointer,compare_function);
一个参数是数组或向量的头指针,第二个向量是尾指针。第三个参数是比较函数的名字。在缺省的时候,默认是大跟堆。(下面的参数都一样就不解释了)
作用:把这一段的数组或向量做成一个堆的结构。范围是(first,last)

然后是pop_heap();

它的函数原型是:void pop_heap(first_pointer,end_pointer,compare_function);

作用:pop_heap()不是真的把最大(最小)的元素从堆中弹出来。而是重新排序堆。它
把first和last交换,然后将[first,last-1)的数据再做成一个堆。

接着是push_heap() void pushheap(first_pointer,end_pointer,compare_function);

作用:push_heap()假设由[first,last-1)是一个有效的堆,然后,再把堆中的新元素加
进来,做成一个堆。

最后是sort_heap()void sort_heap(first_pointer,end_pointer,compare_function);
作用是sort_heap对[first,last)中的序列进行排序。它假设这个序列是有效堆。(当然
,经过排序之后就不是一个有效堆了)

转载:http://blog.csdn.net/hnust_xiehonghao/article/details/9172875


用最大堆做一道ACM题目:POJ 2442:

POJ 2442  http://poj.org/problem?id=2442

Sequence
Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 5910 Accepted: 1813

Description

Given m sequences, each contains n non-negative integer. Now we may select one number from each sequence to form a sequence with m integers. It's clear that we may get n ^ m this kind of sequences. Then we can calculate the sum of numbers in each sequence, and get n ^ m values. What we need is the smallest n sums. Could you help us?

Input

The first line is an integer T, which shows the number of test cases, and then T test cases follow. The first line of each case contains two integers m, n (0 < m <= 100, 0 < n <= 2000). The following m lines indicate the m sequence respectively. No integer in the sequence is greater than 10000.

Output

For each test case, print a line with the smallest n sums in increasing order, which is separated by a space.

Sample Input

1
2 3
1 2 3
2 2 3

Sample Output

3 3 4

Source

POJ Monthly,Guang Lin

给你n*m的矩阵,然后每行取一个元素,组成一个包含n个元素的序列,一共有n^m种序列,

让你求出序列和最小的前n个序列的序列和。

思路  :

来自下面的博客

http://www.cnblogs.com/vip-wy/archive/2012/02/15/2353064.html

解题步骤:

1.将第一序列读入data1向量中,并按升序排序。

2.将数据读入data2向量中,并按升序排序。

    将data2[0] + data1[i] ( 0<=i<=n-1)读入dataq向量中

    用make_heap对dataq建堆。

    然后data2[1] + data1[i] (0<=i<=n-1),如果data2[1] + data1[i]比堆dataq的顶点大,则退出,否则删除

    堆的顶点,插入data2[1] + data1[i]。然后是data2[2],...data2[n - 1]

 3.将dataq的数据拷贝到data1中,并对data1按升序排序

 4.循环2,3步,直到所有数据读入完毕。

 5.打印data1中的数据即为结果。

代码:

#include<algorithm>
#include<iostream>


using namespace std;
bool cmp(int a,int b)
{
	return a>b;
}
int main()
{
	int Case = 0 , i , j , k;
	int n = 0 , m = 0;
	int data1[2001],data2[2001],sum[2001];

	cin >> Case;
	while(Case--)
	{
		cin >> m >> n;
		for(j = 0 ; j < n ; j ++)
		{
			cin >> data1[j];
		}
		for(i = 1 ; i < m ; i ++)
		{
			sort(data1,data1+n);
			for(j = 0 ; j < n ; j ++)
			{
				cin >> data2[j];
			}
			sort(data2,data2+n);

			for(j = 0 ; j < n ; j ++)
			{
				sum[j] = data1[0]+data2[j];
			}
			make_heap(sum,sum+n);
			int temp = 0;
			for(k = 1 ; k < n ; k ++)
			{
				for(j = 0 ; j < n ; j ++)
				{
					temp = data1[k]+data2[j];
					if(temp > sum[0])
					{
						break;
					}
					else
					{
						pop_heap(sum,sum+n);
						sum[n-1] = temp;
						push_heap(sum,sum+n);
					}
				}
			}
			for(j = 0 ; j < n ; j ++)
			{
				data1[j] = sum[j];
			}
		}
		sort(data1,data1+n);  
		for(j=0;j<n-1;j++)  
             printf("%d ",data1[j]);  
         printf("%d\n",data1[j]);
	}
	return 0;	
}

对于堆的优化,可以使用优先队列,见这片文章

priority_queue详细的用法,见这里


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值