zoukankan      html  css  js  c++  java
  • 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[100005],b[100005];
    int main()
    {
        int t,n,cnt,k,m,x,ans;
        cin>>t;
        while(t--)
        {
            cin>>m>>k;
            ans=1;//第一行的时候从1开始
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<k;j++) //a[] 记录上一行加完后的前k个数
                    a[j]=b[j];
                cin>>n;
                cnt=0;
                for(int j=0;j<n;j++)
                {
                    cin>>x;
                    for(int kk=0;kk<ans;kk++)
                        b[cnt++]=a[kk]+x;
                }
                sort(b,b+cnt);
                ans=min(k,cnt);
            }
            for(int i=0;i<k;i++)
            {
                if(i==0)
                    cout<<b[i];
                else
                    cout<<' '<<b[i];
            }
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    PE格式详细讲解10 系统篇10|解密系列
    复杂的数据类型1 C++快速入门07
    复杂的数据类型2 C++快速入门08
    复杂的数据类型2 C++快速入门08
    复杂的数据类型1 C++快速入门07
    PE格式详细讲解10 系统篇10|解密系列
    Win32基础知识1 Win32汇编语言002
    开题篇 Win32汇编语言001
    开题篇 Win32汇编语言001
    Win32基础知识1 Win32汇编语言002
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11289193.html
Copyright © 2011-2022 走看看