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. windows 8 update to windows 8.1

    可以参考以下几个链接: http://blogs.windows.com/windows/b/appbuilder/archive/2013/07/24/windows-8-to-windows-8- ...

  2. spring学习 十七 scope属性,单例多例

    Scope属性是<bean>中的属性,取值可以有, singleton 默认值, 单例, prototype 多例, 每次获取重新实例化, request 每次请求重新实例化, sessi ...

  3. Linux/Python学习路线

    Linux: 初级阶段: 熟练掌握常用80个命令: 掌握Linux常用软件包的安装方法,如源码安装,rpm安装等: 学习安装设备驱动程序(如网卡,显卡驱动): 了解Grub/Lilo引导程序以及简单的 ...

  4. 用模糊查询like语句时如果要查是否包含%字符串该如何写

  5. 2018.11.06 bzoj2287: 【POJ Challenge】消失之物(背包)

    传送门 先假设所有物品都能用,做01背包求出方案数. 然后枚举每个点,分类讨论扣掉它对答案的贡献. 代码: #include<bits/stdc++.h> using namespace ...

  6. s4-3 CSMA

    载波侦听多路访问协议  CSMA:Carrier Sense Multiple Access 特点:"先听后发" 改进ALOHA协议的侦听/发送策略  分类 非持续式 持 ...

  7. 第20章:MongoDB-聚合操作--聚合管道--$unwind

    ①$unwind 在查询数据的时候经常会返回数组信息,但是数组并不方便信息的浏览,所以提供有“$unwind”可以将数组数据变为独立的字符串内容. 将文档中数组类型的字段拆分成多条,每条文档包含数组中 ...

  8. Verilog中的阻塞与非阻塞

    这篇文档值得阅读 按说阻塞与非阻塞是Verilog中最基本的东西,也是老生常谈.但是最近看到很多程序里用到阻塞语句竟然不是很明白,说到底是从来没有自己仔细分析过.当然一般情况程序中也是推荐用非阻塞的. ...

  9. xslt 和一个demo

    https://www.w3.org/1999/XSL/Transform Specifications The XSLT language has three versions which are ...

  10. python advanced programming ( II )

    面向对象编程 简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数.数据封装.继承和多态是面向对象的三大特点. 在Python中,所有数据类型都可以视为对 ...