博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
upc组队赛5 Assembly Required【思维】
阅读量:6293 次
发布时间:2019-06-22

本文共 3142 字,大约阅读时间需要 10 分钟。

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?

输入

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.

输出

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.

样例输入

22 22 1 22 1 33 104 1 5 3 103 2 3 35 1 3 4 6 6

样例输出

2 34 5 5 6 6 7 7 7 7 7

提示

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个最小的数,

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

这样每次遍历复杂度最大也才 m <= 100 * n <= 100 * k <= 100 1e6? (瞎算一通

代码

#include
using namespace std;#define pb push_back#define mp make_pair#define rep(i,a,n) for(int i=a;i
P;typedef long long ll;const int INF =0x3f3f3f3f;const int inf =0x3f3f3f3f;const int mod = 1e9+7;const int MAXN = 105;const int maxn = 10005;int n,m,k;int x;int cnt,tot,flag;int ans[maxn];int pre[maxn];int main() { int t; read(t); while(t--){ memset(ans,0,sizeof ans); //初始化 memset(pre,0,sizeof pre); cnt = 1; read2(m,k); for(int i = 0; i < m; i++){ for(int j = 0; j < k ; j++){ pre[j] = ans[j] ; //pre[] 记录上一行加完后的前k个数 } read(n); tot = 0; for(int j = 0; j < n; j++){ read(x); for(int u = 0; u < cnt ; u++){ ans[tot++] = x + pre[u]; // 输入的数加上上一行的前k个数 } } sort(ans,ans + tot); //从小到大排序 cnt = min(k,tot); //如果cnt 超过了k 那就按k 来算了 } for(int i = 0; i < k; i++){ //取前k个输出 printf("%d%c", ans[i], i == k - 1 ? '\n' : ' '); } }}

转载于:https://www.cnblogs.com/llke/p/10800040.html

你可能感兴趣的文章
Linux_DHCP服务搭建
查看>>
[SilverLight]DataGrid实现批量输入(like Excel)(补充)
查看>>
秋式广告杀手:广告拦截原理与杀手组织
查看>>
翻译 | 摆脱浏览器限制的JavaScript
查看>>
闲扯下午引爆乌云社区“盗窃”乌云币事件
查看>>
02@在类的头文件中尽量少引入其他头文件
查看>>
JAVA IO BIO NIO AIO
查看>>
input checkbox 复选框大小修改
查看>>
BOOT.INI文件参数
查看>>
vmstat详解
查看>>
新年第一镖
查看>>
unbtu使用笔记
查看>>
OEA 中 WPF 树型表格虚拟化设计方案
查看>>
Android程序开发初级教程(一) 开始 Hello Android
查看>>
使用Gradle打RPM包
查看>>
“我意识到”的意义
查看>>
淘宝天猫上新辅助工具-新品填表
查看>>
再学 GDI+[43]: 文本输出 - 获取已安装的字体列表
查看>>
nginx反向代理
查看>>
操作系统真实的虚拟内存是什么样的(一)
查看>>