POJ 2442-Sequence(heap+k路归并)

Sequence

Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 7447   Accepted: 2451

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
题意:给m个长度为n的数组,每次从每个数组中取一个数组成一个长度为m的数组(将数组中的数相加求和),操作n次,即取前n小和。
首先输入第一行(即第一个数组),升序排序,然后接下来输入m-1行,每次输入一行,将第一次输入的数组整体加上这次输入的第一个元素,然后维护成一个最大堆,再然后就要接着筛选,本质是继续维护这个最大堆,一直处理到输入结束即可。
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=2010;
int tem[maxn],ans[maxn],a[maxn];
int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
	{
		scanf("%d%d",&m,&n);
		for(int i=0;i<n;i++)
			scanf("%d",ans+i);
		sort(ans,ans+n);
		for(int k=1;k<m;k++)
		{
			for(int i=0;i<n;i++)
			{
				scanf("%d",a+i);
				tem[i]=ans[i]+a[0];
			}
			make_heap(tem,tem+n);
			for(int i=1;i<n;i++)
				for(int j=0;j<n;j++)
			    {
				    int x=ans[j]+a[i];
				    if(x>=tem[0])break;
				    pop_heap(tem,tem+n);
				    tem[n-1]=x;
				    push_heap(tem,tem+n);
			    }
			    sort_heap(tem,tem+n);
			    for(int i=0;i<n;i++)
					ans[i]=tem[i];
		}
		for(int i=0;i<n;i++)
		    if(i!=n-1)
			printf("%d ",ans[i]);
		    else
			printf("%d\n",ans[i]);
	}
	return 0;
}

时间: 2024-08-07 22:59:03

POJ 2442-Sequence(heap+k路归并)的相关文章

POJ-2442 Sequence (K路归并问题拓展)

题意:有n行含m个元素序列,从每行取一个数得到他们的和,一共可以得到m^n个和.输出前n个最小的和. 思路:可以用优先队列递归解决,当只取前两行的数,得到两个数的和的前n小的序列.这个序列就相当于把第一行和第二行合并,再解决n-1行的子问题. 用优先队列解决的时候也有点小技巧,类似尺取法. //236 KB 563 ms C++ 1480 B #include<cstdio> #include<iostream> #include<cstring> #include&l

POJ 2442 Sequence【堆】

题目链接:http://poj.org/problem?id=2442 题目大意:给出一个m*n的矩阵,从每一行中取出一个数相加,能得到n^m个不同的结果,要求输出其中前n项. 建立一个以n元数组为底层数组的堆,在这里,利用stl中的make_heap,pop_heap,push_heap等函数解决. 1.将第一组数据输入arr1数组,升序排序. 2.将接下来的数据输入到arr2数组中,并且heap[i]=arr1[0]+arr2[0...n-1],make_heap(heap,heap+n).

[ACM] POJ 2442 Sequence (堆的性质)

Sequence Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7011   Accepted: 2262 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

POJ 2442 Sequence(堆的使用练习)

题目地址:POJ 2442 真心没想到这题的思路..原来是从第一行逐步向下加,每次都只保存前n小的数.顺便练习了下堆..不过感觉堆的这种用法用的不太多啊.. 又是手残..把j写成了i,于是就改啊改..改的跟题解上的几乎一样了= = !.. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #inc

算法导论 6.5.9 堆实现K路归并问题

问题: 设计一个时间复杂度为O(NlogK)的算法,它能够将K个有序链表合并为一个有序链表,这里的N为所有输入链表包含的总的元素个数 分析: 该问题为经典的利用堆完成K路归并的问题: 当K个序列满足一定的条件(如单调不减或单调不增)时,利用堆实现K路归并使其归并为一个满足相同条件的 序列,具体做法如下: 1)假设存在K个序列,从每一个序列中取出一个元素放于堆中; 2)从堆中取出顶端元素,并在该元素的序列中取出下一个元素插入堆中. 3)重复操作1)与2),直到完成归并. 具体问题: poj_205

Merge k Sorted Lists, k路归并

import java.util.Arrays; import java.util.List; import java.util.PriorityQueue; /* class ListNode { ListNode next; int val; ListNode(int x) { val = x; } } */ //k路归并问题 public class MergKSortedLists { //二路归并,这个算法时间复杂度o(2n) public ListNode mergeTwoLists

poj 2442 Sequence 优先队列的运用

题意: 给m行,每行n个数,从每行取一个数计算和,求前n小的和. 分析: 优先队列的运用,主要是make_heap,pop_heap,push_heap三个STL函数的用法. 代码: //poj 2442 //sep9 #include <iostream> #include <algorithm> using namespace std; const int maxN=2048; int a[maxN],b[maxN],sum[maxN]; int main() { int ca

poj 2442 Sequence

Sequence Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7019   Accepted: 2266 题目大意:给出n个序列每一个序列有每一个元素在每一个序列里取出一个元素求和  输出前n个最小和 优先队列 #include<cstring> #include<cstdio> #include<cstring> #include<queue> #include<iostr

k路归并 算法导论8-4(e)

使用STL中priority_queue(由最大最小堆实现的)来实现.注意传递参数的时候需要传递三个. 模板声明:priority_queue<Type, Container, Functional> 这里的实现要求输入完全正确,所以代码移植性非常差. #include <iostream> #include <algorithm> #include <queue> using namespace std; typedef struct { int key;