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,在每个序列中各取一个数并求和,输出前m小的和

解题思路

共n*m种取法,显然是个求k小堆的问题,但我用了优先队列直接写了,不过据说,二分比优先队列快好多...

贡献些数据吧:

10 10
21 12 123 3 21 123 32 143 43 56
2 32 43 34 54 56 656 76 43 234
234 45 5 65 56 76 43 23 435 57
32 324 435 46 56 76 87 78 43 23
3 32 324 45 56 57 34 23 54 565
23 32 34 342 324 232 2 432 324 12
234 324 4 45 65 67 435 23 5 654
34 3245 345 56 56 657 67 456 345 325
234 234 546 65 88 66 53 654 65 765
5 3 34 34 34 56 345 234 2 34
答案是:131 132 132 133 134 135 140 140 141 141

AC代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int a[],num[],n,m,t;
void solve()
{
priority_queue<int >q;
for(int i=;i<m;i++) q.push(a[i]+num[]);///将num序列最小的放进去
for(int i=;i<m;i++)
for(int j=;j<m;j++)
{
int x=a[i]+num[j];///遍历加和
if(x<q.top()) q.push(x),q.pop();
else break;///从小到大排序 所以第一个小的不符合后面就不用看了
}
for(int i=m-;i>=;i--) /// 因为大的优先级高 所以倒序存入a继续参加之后的加和过程
{
a[i]=q.top(); q.pop();
}
}
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=;i<m;i++) scanf("%d",&a[i]);
sort(a,a+m);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++) cin>>num[j];
sort(num,num+m);
solve();
}
for(int i=;i<m;i++) printf("%d%c",a[i],i==m-?'\n':' ');
}
return ;
}

POJ 2442(优先队列 k路归并 堆)的更多相关文章

  1. 使用最小堆来完成k路归并 6.5-8

    感谢:http://blog.csdn.net/mishifangxiangdefeng/article/details/7668486 声明:供自己学习之便而收集整理 题目:请给出一个时间为O(nl ...

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

    问题: 设计一个时间复杂度为O(NlogK)的算法,它能够将K个有序链表合并为一个有序链表,这里的N为所有输入链表包含的总的元素个数 分析: 该问题为经典的利用堆完成K路归并的问题: 当K个序列满足一 ...

  3. 多线程外排序解决大数据排序问题2(最小堆并行k路归并)

    转自:AIfred 事实证明外排序的效率主要依赖于磁盘,归并阶段采用K路归并可以显著减少IO量,最小堆并行k路归并,效率倍增. 二路归并的思路会导致非常多冗余的磁盘访问,两组两组合并确定的是当前的相对 ...

  4. HDU - 6041:I Curse Myself(Tarjan求环&K路归并)

    There is a connected undirected graph with weights on its edges. It is guaranteed that each edge app ...

  5. k路归并(败者树,记录败者)

          败者树在外排序中用到,每加入一个数字时,调整树需要o(lgk),比较快.外排序过程主要分为两个阶段:(1)初始化各归并段写入硬盘,初识化的方法,可利用内排序方法还可以一种叫置换选择排序的方 ...

  6. Merge k Sorted Lists, k路归并

    import java.util.Arrays; import java.util.List; import java.util.PriorityQueue; /* class ListNode { ...

  7. 【二叉堆】k路归并问题(BSOJ1941)

    Description 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Ai*x^2+Bi*x+Ci(x∈N*).给定这些Ai.Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复 ...

  8. POJ-2442 Sequence K路归并问题

    题目链接:http://poj.org/problem?id=2442 问题一:K个有序表合成一个有序表,元素共有n个.用堆优化 问题二:两个序列的前n小的元素.堆优化. 这题就是问题二的扩展,每次处 ...

  9. k路归并

    public static int[] k_merge(ArrayList<int[]> k_array) { if(CollectionUtils.isEmpty(k_array)){ ...

随机推荐

  1. CRC在线计算工具

    http://www.lammertbies.nl/comm/info/crc-calculation.html

  2. rails 表单中默认值

    在表单中加入默认提示值,如(email@email.com): <div class="field"> <%= form.label :email,"E ...

  3. shell脚本清空redis库缓存

    前提: 现在做的一个业务系统,用了redis做缓存. 系统做了缓存,通常在系统正常使用的过程中,可以节省很多系统资源,特别是数据库资源. 但是,在开发.测试或者系统遇到问题的时候,也有很麻烦的事情. ...

  4. Eclipse中配置约束

    一.本地配置schema约束(xsd文件): 1.比如配置spring的applicationContext.xml中的约束条件: 复制applicationContext.xml中如图: 2.win ...

  5. Vue自定义指令报错:Failed to resolve directive: xxx

    Vue自定义指令报错 Failed to resolve directive: modle 这个报错有2个原因: 1.指令单词拼错 2.Vue.directive() 这个方法没有写在 new Vue ...

  6. ssh scp 加端口

    scp -P one-infrastructure-api.tar.gz console@172.31.16.2:/root/ ssh -p console@172.31.16.2

  7. IntellJ IDEA2017 springboot2.0.2 替代@SpringBootApplication方式

    如果不想用@SpringBootApplication,那么可以用@EnableAutoConfiguration 和@ComponentScan替代@SpringBootApplication 详情 ...

  8. oracle 操作,偶尔记一下

    一.根据更新时间排序 1.插入insert into t_user t (t.user_id,t.user_name,t.create_time,t.update_time)values('10',' ...

  9. python中的\n、\r与\b

    python中使用print函数,有时候会使用end参数来控制字符输出效果,这时候\n.\r与\b就派上用场了. \n 代表换行,也就是从本行换到下一行 \r 代表回车,也就是回到本行最开始的位置,从 ...

  10. .net 打开Excel文档并转为DataTable

    /// <summary> /// 打开Excel文档并转为DataTable /// </summary> /// <returns></returns&g ...