pku 2442 Sequence stl堆的应用
2012-04-01 19:50
323 查看
http://poj.org/problem?id=2442
题意很简单,思路就是任意的两个含有m个元素的序列求和,用o(nlgn)的堆来维护n个最小元素,最终得到的丢里面的元素就是所要求的。。。
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)中的序列进行排序。它假设这个序列是有效堆。(当然
,经过排序之后就不是一个有效堆了)
应用说明:
View Code
题意很简单,思路就是任意的两个含有m个元素的序列求和,用o(nlgn)的堆来维护n个最小元素,最终得到的丢里面的元素就是所要求的。。。
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)中的序列进行排序。它假设这个序列是有效堆。(当然
,经过排序之后就不是一个有效堆了)
应用说明:
View Code
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define maxn 2007 using namespace std; int a[maxn],b[maxn],head[maxn]; int n,m; int main() { int t,i,j; scanf("%d",&t); while (t--) { scanf("%d%d",&m,&n); for (i = 0; i < n; ++i) scanf("%d",&a[i]); sort(a,a + n); while (--m) { for (i = 0; i < n; ++i) scanf("%d",&b[i]); sort(b,b + n); for (i = 0; i < n; ++i) head[i] = a[i] + b[0]; make_heap(head,head + n); for (i = 1; i < n; ++i) { bool flag = false; for (j = 0; j < n; ++j) { int tmp = a[j] + b[i]; if (tmp < head[0]) { pop_heap(head,head + n); head[n - 1] = tmp; push_heap(head,head + n); flag = true; } else break; } if (!flag) break; } for (i = 0; i < n; ++i) a[i] = head[i]; sort(a,a + n); } for (i = 0; i < n - 1; ++i) printf("%d ",a[i]); printf("%d\n",a[n - 1]); } return 0; }
相关文章推荐
- pku2442 Sequence
- POJ 2442 Sequence(堆的应用)
- POJ 2442 Sequence
- POJ 题目2442 Sequence(技巧,优先队列)
- pku 1699 Best Sequence 状态压缩dp
- pku2480(欧拉函数的应用,推公式,积性函数)
- pku2593MaxSequence
- pku 2623 sequence median 水题 解题报告
- POJ-2442 Sequence (K路归并问题拓展)
- POJ 2442Sequence
- poj 2442 Sequence
- POJ 2442 Sequence 堆
- [ACM] POJ 2442 Sequence (堆的性质)
- poj 2442 Sequence
- poj 2442 -- Sequence
- u-boot中typedef应用解析___init_fnc_t *init_sequence[]
- POJ 2442 Sequence
- Poj 2442 Sequence
- POJ 2442 Sequence(堆的使用练习)
- POJ 2442 Sequence(堆的使用练习)