stl 堆的简介 及poj2442

来源:互联网 发布:程序员真的会短命吗 编辑:程序博客网 时间:2024/06/05 20:30

最大堆最小堆代码实现

http://blog.csdn.NET/xiaoxiaoxuewen/article/details/7570621

最大堆 最小堆原理图

http://www.cnblogs.com/wu8685/archive/2010/12/30/1922218.html

STL 的堆操作

http://hi.baidu.com/solofancy/item/14acd02b9743f7d30f37f927 

http://blog.csdn.net/morewindows/article/details/6967409


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)中的序列进行排序。它假设这个序列是有效堆。(当然
,经过排序之后就不是一个有效堆了)

实例:

[cpp] view plain copy
  1. #include<algorithm>  
  2. #include<cstdio>  
  3. using namespace std;  
  4. bool cmp(int a,int b)  
  5. {  
  6.     return a>b;  
  7. }  
  8. int main()  
  9. {  
  10.     int i,number[20]={29,23,20,22,17,15,26,51,19,12,35,40};   
  11.     make_heap(&number[0],&number[12]);    
  12.     //结果是:51 35 40 23 29 20 26 22 19 12 17 15     
  13.     for(i=0;i<12;i++)          
  14.         printf("%d ",number[i]);  
  15.     printf("\n");     
  16.     make_heap(&number[0],&number[12],cmp);  
  17.     //结果:12 17 15 19 23 20 26 51 22 29 35 40  
  18.     for(i=0;i<12;i++)          
  19.         printf("%d ",number[i]);      
  20.     printf("\n");     
  21.     //加入元素8   
  22.     number[12]=8;     
  23.     //加入后调整   
  24.     push_heap(&number[0],&number[13],cmp);    
  25.     //结果:8 17 12 19 23 15 26 51 22 35 40 20   
  26.     for(i=0;i<13;i++)          
  27.         printf("%d ",number[i]);      
  28.     printf("\n");     
  29.     //弹出元素8   
  30.     pop_heap(&number[0],&number[13],cmp);     
  31.     //结果:12 17 15 19 23 20 26 51 22 29 35 40 8    
  32.     for(i=0;i<13;i++)          
  33.         printf("%d ",number[i]);      
  34.     printf("\n");     
  35.     sort_heap(&number[0],&number[12],cmp);    
  36.     //结果不用说都知道是有序的了!      
  37.     for(i=0;i<12;i++)          
  38.         printf("%d ",number[i]);      
  39.     return 0;     
  40. }  




应用

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

Sequence
Time Limit: 6000MS Memory Limit: 65536KTotal 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

12 31 2 32 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中的数据即为结果。

[cpp] view plain copy
  1. #include<cstdio>  
  2.  #include<algorithm>  
  3.  #include<iostream>  
  4.  #define Max 2008  
  5.  using namespace std;  
  6.  int a[Max],b[Max],sum[Max];  
  7.  int main()  
  8.  {  
  9.      int i,j,k,t,m,n,temp;  
  10.      scanf("%d",&t);  
  11.      while(t--)  
  12.      {  
  13.          scanf("%d %d",&m,&n);  
  14.          for(i=0;i<n;i++)  
  15.              scanf("%d",&a[i]);  
  16.          for(i=1;i<m;i++)  
  17.          {  
  18.              sort(a,a+n);  
  19.              for(j=0;j<n;j++)  
  20.                  scanf("%d",&b[j]);  
  21.              sort(b,b+n);  
  22.              for(j=0;j<n;j++)  
  23.                  sum[j]=a[j]+b[0];  
  24.              make_heap(sum,sum+n);  
  25.              for(j=1;j<n;j++)  
  26.              {  
  27.                  for(k=0;k<n;k++)  
  28.                  {  
  29.                      temp=a[k]+b[j];  
  30.                      if(temp>=sum[0])  
  31.                          break;  
  32.                      pop_heap(sum,sum+n);  
  33.                      sum[n-1]=temp;  
  34.                      push_heap(sum,sum+n);  
  35.                  }  
  36.              }  
  37.              for(j=0;j<n;j++)  
  38.                      a[j]=sum[j];  
  39.          }  
  40.          sort(a,a+n);  
  41.          for(j=0;j<n-1;j++)  
  42.              printf("%d ",a[j]);  
  43.          printf("%d\n",a[j]);  
  44.      }return 0;  
  45.  }  


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


0 0
原创粉丝点击