Problem A: Assembly Required

Princess Lucy broke her old reading lamp, and needs a new one. The castle orders a shipment of parts from the Slick Lamp Parts Company, which produces interchangable lamp pieces.
There are m types of lamp pieces, and the shipment contained multiple pieces of each type. Making a lamp requires exactly one piece of each type. The princess likes each piece with some value, and she likes a lamp as much as the sum of how much she likes each of the pieces.
You are part of the castle staff, which has gotten fed up with the princess lately. The staff needs to propose k distinct lamp combinations to the princess (two lamp combinations are considered distinct if they differ in at least one piece). They decide to propose the k combinations she will like the least. How much will the princess like the k combinations that the staff proposes?

Input

The first line of input contains a single integer T (1 ≤ T ≤ 10), the number of test cases. The first line of each test case contains two integers m (1 ≤ m ≤ 100), the number of lamp piece types and k (1 ≤ k ≤ 100), the number of lamps combinations to propose. The next m lines each describe the lamp parts of a type; they begin with ni (2 ≤ ni ≤ 100), the number of pieces of this type, followed by ni integers vi,1,...,vi,ni (1 ≤ vi,j ≤ 10,000) which represent how much the princess likes each piece. It is guaranteed that k is no greater than the product of all ni’s.

Output

For each test case, output a single line containing k integers that represent how much the princess will like the proposed lamp combinations, in nondecreasing order.
Sample Input

2

2 2

2 1 2

2 1 3

3 10

4 1 5 3 10

3 2 3 3

5 1 3 4 6 6

Sample Output

2 3

4 5 5 6 6 7 7 7 7 7

Explanation
In the first case, there are four lamp pieces, two of each type. The worst possible lamp has value 1 + 1 = 2, while the second worst possible lamp has value 2 + 1 = 3.

题意:

第一行一个样例数t

第二行 m 和 k

接下来是m行 第一个数字n 表示这行有n个数

要求从每行选一个数 组成一个数

求前k个最小的数

思路 :如果一行选一个再比较这样肯定不行啦

既然我们只要前k个最小的

那么只需要把一行的每个数字去加上上一行求出的前k个最小的数,

因为最小值肯定是从这些数里产生

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;
int a[],b[];
int main()
{
int t,n,cnt,k,m,x,ans;
cin>>t;
while(t--)
{
cin>>m>>k;
ans=;//第一行的时候从1开始
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(int i=;i<m;i++)
{
for(int j=;j<k;j++) //a[] 记录上一行加完后的前k个数
a[j]=b[j];
cin>>n;
cnt=;
for(int j=;j<n;j++)
{
cin>>x;
for(int kk=;kk<ans;kk++)
b[cnt++]=a[kk]+x;
}
sort(b,b+cnt);
ans=min(k,cnt);
}
for(int i=;i<k;i++)
{
if(i==)
cout<<b[i];
else
cout<<' '<<b[i];
}
cout<<endl;
}
return ;
}

Problem A: Assembly Required 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. k路归并(败者树,记录败者)

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

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

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

  5. Merge k Sorted Lists, k路归并

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

  6. 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 ...

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

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

  8. POJ 2442(优先队列 k路归并 堆)

    Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...

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

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

随机推荐

  1. 【PAT甲级】1054 The Dominant Color (20 分)

    题意: 输入两个正整数M和N(M<=800,N<=600),分别代表一张图片的宽度和高度,接着输入N行每行包括M个点的颜色编号,输出这张图片主导色的编号.(一张图片的主导色占据了一半以上的 ...

  2. 使用YUM安装软件时提示PackageKit睡眠中解决方法!

    报错如图所示: 解决方法一:移除var/run/yum.pid文件 方法二:直接杀掉进程号 报错的时候会跟进程号 直接利用kill   -9  +进程号

  3. redhat 7.6 安装 git

    1.下载git包 下载网址:https://mirrors.edge.kernel.org/pub/software/scm/git/   //自己下载想要的版本 或者直接在Linux 直接使用wge ...

  4. Go语言学习笔记(四)

    一.字符串 1.字符串截取 可以使用len(字符串变量)获取字符串的字节长度,其中英文占1个字节长度,中文占用3个字节长度 可以使用变量名[n]获取到字符串第n+1个字节,返回这个字节对应的Unico ...

  5. Python - 工具

    Anaconda - 自带Conda,可以自定义环境 Pycharm zeal - API离线查看,类似于Dash

  6. JavaScript判断两个对象内容是否相等

    ES6中有一个方法判断两个对象是否相等,这个方法判断是两个对象引用地址是否一致 let obj1= { a: 1 } let obj2 = { a: 1 } console.log(Object.is ...

  7. cross_val_score 交叉验证与 K折交叉验证,嗯都是抄来的,自己作个参考

    因为sklearn cross_val_score 交叉验证,这个函数没有洗牌功能,添加K 折交叉验证,可以用来选择模型,也可以用来选择特征 sklearn.model_selection.cross ...

  8. ROS-debug1 : 运行roscore时报错:Unable to contact my own server at...

    一.问题描述 在终端运行roscore时,出现错误:Unable to contact my own server at...,如下图: 二.解决方法 以上问题是由于ROS环境变量ROS_MASTER ...

  9. Python 的直接赋值、Deepcopy、Copy的区别

    直接赋值: 其实就是对象的引用 浅拷贝(copy): 只拷贝符对象,不会拷贝对象内部的子对象 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象 有图有 ...

  10. Java基础 -4.6

    循环嵌套 乘法口诀表 public static void main(String[] args) { for(int x =1;x<10;x++) { for(int y=1;y<=x; ...