STL--F-sequence (N * m-> calculate the smallest first m and)

Source: Internet
Author: User

F-Sequence Time limit:6000 ms Memory limit:65536kb 64bit Io format:% I64d & % i64usubmit status

Description

Given m sequences, each contains N non-negative integer. now we may select one number from each sequence to form a sequence with M integers. it's clear that we may get n ^ m this kind of sequences. then we can calculate the sum of numbers in each sequence, and get n ^ m values. what we need is the smallest n sums. cocould you help us?

Input

The first line is an integer T, which shows the number of test cases, and then t test cases follow. the first line of each case contains two integers m, n (0 <m <= 100, 0 <n <= 2000 ). the following M lines indicate the M sequence respectively. no integer in the sequence is greater than 10000.

Output

For each test case, print a line with the smallest n sums in increasing order, which is separated by a space.

Sample Input

12 31 2 32 2 3

Sample output

3 3 4

Thanks http://www.cnblogs.com/372465774y/archive/2012/07/09/2583866.html

First, let's think about two questions.



From these two results, we need to obtain m values for each array of N arrays, and find the smallest m values for the sum of array 1 and array 2, and then sum with array 3 to find the smallest m values, finally, we get the smallest m of all arrays.

Since each array is ordered, and we require the smallest M, array a [I] [J] + values in the queue> the first value of the queue, then, the value after a [I] [J] And the queue will be greater than the first, which is meaningless for the minimum m values to be solved. The queue stores the smallest m Sum of the current array to all previous arrays, and constantly updates the queue.

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;int a[110][2100] , b[2100] ;priority_queue <int> p ;int main(){    int i , j , k , n , m , t ;    scanf("%d", &t);    while(t--)    {        scanf("%d %d", &n, &m);        for(i = 0 ; i < n ; i++)        {            for(j = 0 ; j < m ; j++)                scanf("%d", &a[i][j]);            sort(a[i],a[i]+m);        }        for(i = 0 ; i < m ; i++)            p.push(a[0][i]) ;        for(i = 1 ; i < n ; i++)        {            for(j = 0 ; j < m ; j++)            {                b[j] = p.top();                p.pop();            }            for(j = 0 ; j < m ; j++)            {                for(k = m-1 ; k >= 0 ; k--)                {                    if(j == 0)                        p.push( a[i][j]+b[k] );                    else                    {                        if( a[i][j] + b[k] < p.top() )                        {                            p.pop();                            p.push(a[i][j]+b[k]);                        }                        else                            break;                    }                }            }        }        for(j = 0 ; j < m ; j++)        {            b[j] = p.top();            p.pop();        }        for(j = m-1 ; j >= 0 ; j--)        {            if(j == 0)                printf("%d\n", b[j]);            else                printf("%d ", b[j]);        }    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.