Problem A: Assembly Required K路归并
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路归并的更多相关文章
- 使用最小堆来完成k路归并 6.5-8
感谢:http://blog.csdn.net/mishifangxiangdefeng/article/details/7668486 声明:供自己学习之便而收集整理 题目:请给出一个时间为O(nl ...
- 算法导论 6.5.9 堆实现K路归并问题
问题: 设计一个时间复杂度为O(NlogK)的算法,它能够将K个有序链表合并为一个有序链表,这里的N为所有输入链表包含的总的元素个数 分析: 该问题为经典的利用堆完成K路归并的问题: 当K个序列满足一 ...
- k路归并(败者树,记录败者)
败者树在外排序中用到,每加入一个数字时,调整树需要o(lgk),比较快.外排序过程主要分为两个阶段:(1)初始化各归并段写入硬盘,初识化的方法,可利用内排序方法还可以一种叫置换选择排序的方 ...
- 多线程外排序解决大数据排序问题2(最小堆并行k路归并)
转自:AIfred 事实证明外排序的效率主要依赖于磁盘,归并阶段采用K路归并可以显著减少IO量,最小堆并行k路归并,效率倍增. 二路归并的思路会导致非常多冗余的磁盘访问,两组两组合并确定的是当前的相对 ...
- Merge k Sorted Lists, k路归并
import java.util.Arrays; import java.util.List; import java.util.PriorityQueue; /* class ListNode { ...
- 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 ...
- POJ-2442 Sequence K路归并问题
题目链接:http://poj.org/problem?id=2442 问题一:K个有序表合成一个有序表,元素共有n个.用堆优化 问题二:两个序列的前n小的元素.堆优化. 这题就是问题二的扩展,每次处 ...
- POJ 2442(优先队列 k路归并 堆)
Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...
- 【二叉堆】k路归并问题(BSOJ1941)
Description 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Ai*x^2+Bi*x+Ci(x∈N*).给定这些Ai.Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复 ...
随机推荐
- 洛谷 P2925 [USACO08DEC]干草出售Hay For Sale
嗯... 题目链接:https://www.luogu.org/problemnew/show/P2925 这是一道简单的01背包问题,但是按照正常的01背包来做会TLE一个点,所以要加一个特判(见代 ...
- C++98常用特性介绍——mutable关键字
讲mutable前,先讲一下const函数,讲const函数前,先讲一下函数前后加const的区别 一.C++函数前后加const的区别 1)函数前加const:普通函数或非静态成员函数前均可加con ...
- BinaryTree(二叉树) - 再谈二叉树
经过两天的研究,总算是完全梳理清二叉树的基本操作了,然后我又发现了一些对二叉树的新的认识. 先具体说说删除操作,前面在对二叉树的补充中,我说到了二叉树的删除操作可以有两种不同的代码编写方式(可点这里去 ...
- re.compile匹配
import re string = '<h4 class="title">愤怒的葡萄</h4>' pattern = '<h4 class=&quo ...
- ShellCode模板
前言 在上一篇文章上使用到的添加用户的shellcode是怎么得到的呢? 先来拆分一下汇编的功能 ;寻找kernel32.dll的基地址 xor ecx,ecx mov eax,dword ptr f ...
- 关于自学java的内容及感受
这周自学了关于java输入的知识:java输入的方法与c++和c有些不同,他需要在开头加一个import连接系统的包,才能进行输入语句的编写. 自己编写了一点简单的输入的程序: package mod ...
- PAT T1008 Airline Routes
用tarjan算法缩点~ #include<bits/stdc++.h> using namespace std; ; vector<int> g[maxn]; int N,M ...
- 文件目录T位
场景: 共享目录设置T标志 可以看别人的文件,但不可以删除.修改别人的文件 除非是ROOT,目录的拥有者
- 十一 Spring的AOP开发的相关术语
SpringAOP简介: AOP思想最早是由AOP联盟组织提出的.Spring使用这种思想最好的框架. Spring的AOP有自己实现的方式,但是非常繁琐.AspectJ是一个AOP框架,Spring ...
- Spring boot 中发送邮件
参考:https://blog.csdn.net/qq_39241443/article/details/81293939 添加依赖: <dependency> <groupId&g ...