upc组队赛5 Assembly Required【思维】
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.
样例输入
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
样例输出
2 3
4 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<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define rep(i,a,n) for(int i=a;i<n;++i)
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define sca(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
typedef pair<int,int> 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' : ' ');
}
}
}
upc组队赛5 Assembly Required【思维】的更多相关文章
- Assembly Required【思维】
问题 A: Assembly Required 时间限制: 1 Sec 内存限制: 128 MB 提交: 49 解决: 25 [提交] [状态] [命题人:admin] 题目描述 Princess ...
- Problem A: Assembly Required K路归并
Problem A: Assembly Required Princess Lucy broke her old reading lamp, and needs a new one. The cast ...
- upc组队赛2 Hakase and Nano【思维博弈】
Hakase and Nano 题目描述 Hakase and Nano are playing an ancient pebble game (pebble is a kind of rock). ...
- upc组队赛6 Canonical Coin Systems【完全背包+贪心】
Canonical Coin Systems 题目描述 A coin system S is a finite (nonempty) set of distinct positive integers ...
- upc组队赛3 Chaarshanbegaan at Cafebazaar
Chaarshanbegaan at Cafebazaar 题目链接 http://icpc.upc.edu.cn/problem.php?cid=1618&pid=1 题目描述 Chaars ...
- upc组队赛3 Iranian ChamPions Cup
Iranian ChamPions Cup 题目描述 The Iranian ChamPions Cup (ICPC), the most prestigious football league in ...
- upc组队赛1 过分的谜题【找规律】
过分的谜题 题目描述 2060年是云南中医学院的百年校庆,于是学生会的同学们搞了一个连续猜谜活动:共有10个谜题,现在告诉所有人第一个谜题,每个谜题的答案就是下一个谜题的线索....成功破解最后一个谜 ...
- upc组队赛1 不存在的泳池【GCD】
不存在的泳池 题目描述 小w是云南中医学院的同学,有一天他看到了学校的百度百科介绍: 截止到2014年5月,云南中医学院图书馆纸本藏书74.8457万册,纸质期刊388种,馆藏线装古籍图书1.8万册, ...
- upc组队赛1 黑暗意志【stl-map】
黑暗意志 题目描述 在数千年前潘达利亚从卡利姆多分离之时,迷雾笼罩着这块新形成的大陆,使它不被外来者发现.迷雾同样遮蔽着这片大陆古老邪恶的要塞--雷神的雷电王座.在雷神统治时期,他的要塞就是雷电之王力 ...
随机推荐
- 【读书笔记】:MIT线性代数(4):Independence, Basis and Dimension
Independence: The columns of A are independent when the nullspace N (A) contains only the zero vecto ...
- WireShark 自带工具 editcap 和 text2pcap 配合完成改包操作
一.拆包 首先声明这种方法比较复杂而且需要点技术水平,不建议菜鸟尝试(可以使用WireEdit编辑pcap包,不过要联网)其实在熟练这种方法后也可以很快的,但这种方法主要还是方便吧,不用下载其他什么软 ...
- RSTP生成树
一.实验目的 二.实验拓扑图 三.实验编址 四.实验步骤 1.基本步骤 配置PC机IP 配置完成,开启所有设备,测试主机之间连通性 2.配置RSTP基本功能 在四台交换机上修改生成树模式:配置完成后, ...
- 数据库(四)—— Redis数据库
目录 redis 一.redis的安装部署 二.redis配置文件 1.基本配置 2.安全配置 3.查看配置信息 三.redis持久化 1.持久化类型 2.RDB持久化 3.AOF持久化(append ...
- Cocos2d 之FlyBird开发---GameUnit类
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 这节来实现GameUnit类中的一些函数方法,其实这个类一般是一个边写边完善的过程,因为一般很难一次性想全所有的能够供多个类共用的方法.下 ...
- loadrunner 快捷键
lr 不同的界面切换 crt f6lr:选中要查找的内容 ctrl +f3 超找文本 选中所有字母大写 ctl +shift+ u;小写
- 【Nacos】数据一致性
转自:https://blog.csdn.net/liyanan21/article/details/89320872 目录 一.Raft算法 二.Nacos中Raft部分源码 init() 1. 获 ...
- C#设计模式V2(1)——单例模式
出自:https://www.cnblogs.com/zhili/p/SingletonPatterm.html 一.引言 最近在设计模式的一些内容,主要的参考书籍是<Head First 设计 ...
- 2019-1-28-WPF-高性能笔
title author date CreateTime categories WPF 高性能笔 lindexi 2019-1-28 14:21:5 +0800 2018-2-13 17:23:3 + ...
- Android Paint类介绍以及浮雕和阴影效果的设置(转)
转自:https://blog.csdn.net/lpjishu/article/details/45558375 Paint类介绍 Paint即画笔,在绘制文本和图形用它来设置图形颜色, 样式等绘制 ...