问题 A: Assembly Required

时间限制: 1 Sec  内存限制: 128 MB

提交: 49  解决: 25

[提交] [状态] [命题人:admin]

题目描述

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' : ' ');
      }
    }
}

Assembly Required【思维】的更多相关文章

  1. upc组队赛5 Assembly Required【思维】

    Assembly Required 题目描述 Princess Lucy broke her old reading lamp, and needs a new one. The castle ord ...

  2. Problem A: Assembly Required K路归并

    Problem A: Assembly Required Princess Lucy broke her old reading lamp, and needs a new one. The cast ...

  3. .NET的类型层次查看工具

    上周为了快速了解一个.NET的库而需要查看其类型层次.假如要在文章中表示一个类型层次,还是用文本比较舒服,截图始终是不方便.Reflector虽然能够显示类型层次,但我无法方便的把显示出来的类型层次转 ...

  4. asp.net/wingtip/创建数据访问层

    一. 什么是数据访问层 在wingtip项目中,数据访问层是对以下三者的总称:1. product类等数据相关的实体类(class)2. 数据库(database),对实体类成员的存储3. 上述二者的 ...

  5. [Golang] GoConvey测试框架使用指南

    GoConvey 是一款针对Golang的测试框架,可以管理和运行测试用例,同时提供了丰富的断言函数,并支持很多 Web 界面特性. GoConvey 网站 : http://smartystreet ...

  6. Python爬虫之抓取豆瓣影评数据

    脚本功能: 1.访问豆瓣最受欢迎影评页面(http://movie.douban.com/review/best/?start=0),抓取所有影评数据中的标题.作者.影片以及影评信息 2.将抓取的信息 ...

  7. Dynamics 365 Customer Engagement导入解决方案时出错:Microsoft.Crm.CrmException: Plug-in assembly does not contain the required types or assembly content cannot be updated.

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  8. 为C# as 类型转换及Assembly.LoadFrom埋坑!

    背景: 不久前,我发布了一个调试工具:发布:.NET开发人员必备的可视化调试工具(你值的拥有) 效果是这样的: 之后,有小部分用户反映,工具用不了(没反应或有异常)~~~ 然后,建议小部分用户换个电脑 ...

  9. Assembly - Registers

    Processor operations mostly involve processing data. This data can be stored in memory and accessed ...

随机推荐

  1. hibernate08--OpenSessionInView

    创建一个web项目,然后生成HibernateSessionFactory文件! package cn.bdqn.util; import org.hibernate.HibernateExcepti ...

  2. Assignments---(贪心)

    Assignments Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. 【转载】解决gridview空行时不显示的问题

    问题: GridView控件应用很是广泛,通常将它与DataSourceControl搭配使用,当然也可以手工指定DataSource属性来完成数据绑定.如果数据源返回一个空行的数据集(例如查询不到指 ...

  4. Markdown编辑工具及命令

    Markdown是一种可以使用普通文本编辑器编辑的标记语言,通过使用简单的编辑,可以使文本具有一定的格式. Typora是一款简介的Markerdown编辑器. 文本编辑语法: 标题: # 一阶标题 ...

  5. Eclipse 00: 常用快捷键

    Eclipse常用快捷键 1几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/)快速修正:Ctrl+1单词补全:Alt+/打开外部Java文档:Shift+F2 显示搜索 ...

  6. 转载:Linux下解压zip乱码问题的解决(unzip)

    https://blog.csdn.net/abyjun/article/details/48344379 在windows上压缩的文件,是以系统默认编码中文来压缩文件.由于zip文件中没有声明其编码 ...

  7. SVProgressHUD提示框IOS

    SVProgressHUD--比MBProgressHUD更好用的 iOS进度提示组件 项目里用到SVProgressHud,感觉背景颜色太丑,因为很久很久以前改过,就想在这个项目里也改下,但是时间过 ...

  8. IO调度算法的选择

    一) I/O调度程序的总结 1) 当向设备写入数据块或是从设备读出数据块时,请求都被安置在一个队列中等待完成. 2) 每个块设备都有它自己的队列. 3) I/O调度程序负责维护这些队列的顺序,以更有效 ...

  9. C工程 交互 ceph 分布式存储系统

    网上看到有人问,如何在C项目里调用ceph系统对外提供的API,实现分布式存储. 我在网上搜到了相关信息,但是因为不是会员无法追加答案,故而,贴于此. 赠予有缘人:) ———————————————— ...

  10. 关于绕过域名(ip)校验的一些小知识

    这篇文章最开始只是想写一个关于绕过referer的方法,写着写着发现和ssrf以及url跳转的一些手法类似,于是把这两种也加上了 对referer做校验一般是对csrf进行防范的手段之一,但是很多时候 ...