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. Android Studio 使用入门及问题汇总

    声明:转载自http://blog.csdn.net/wei_chong_chong/article/details/56280383 之前一直用eclipse+adt做Android开发.曾经尝试使 ...

  2. 洛谷 P1339 [USACO09OCT]热浪Heat Wave(最短路)

    嗯... 题目链接:https://www.luogu.org/problem/P1339 这道题是水的不能在水的裸最短路问题...这里用的dijkstra 但是自己进了一个坑—— 因为有些城市之间可 ...

  3. MYSQL优化考虑十个方面

    1)索引 2)sql优化 3)锁 4)延迟 5)参数优化 6)连接数 7)cpu 8)iops 9)磁盘 10)内存

  4. mongodb的remove操作

    今天学习mongodb时,打算用db.user.remove()函数把user中的数据都删了,结果没闪成功,提示:remove needs a query.上网查了一下,是因为没有给remove函数传 ...

  5. 基于Goolgle最新NavigationDrawer实现全屏水平平移

    常见实现App 上面侧边栏菜单之前使用SlidingMenu,现在发现Goolgle原生NavigationDrawer也挺好用.但是细心的开发者们发现NavigationDrawer没有类似Slid ...

  6. LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第N个节点)

    题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->n ...

  7. Linux Kernel 5.5 最终删除 SYSCTL 系统调用

    导读 Linux Kernel 5.5 最终消除了支持sysctl系统调用的代码,该代码已被弃用了大约十年,目前对任何体系结构的现代系统都没有影响. 长期以来,Linux sysctl系统调用都不建议 ...

  8. Codeforces Round #588 (Div. 2)C(思维,暴力)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[27],b[27];int vis ...

  9. vue-cli 手脚架mock虚拟数据的运用,特别是坑!!!

    1.现在基本的趋势就是前后分离,前后分离就意味着当后台接口还没完成之前,前端是没有接口可以拿来调用的 ,那么mock虚拟数据就很好的解决了这一问题,前端可以直接模拟真实的数据AJAX请求! 运用 步骤 ...

  10. 【快学springboot】14.操作redis之list

    前言 之前讲解了springboot(StringRedisTemplate)操作redis的string数据结构,这篇文章将会讲解list数据结构 list数据结构具有的操作 下图列出了redis ...