Sequence (priority queue)

Source: Internet
Author: User
Sequence
Time Limit: 6000MS Memory Limit: 65536K
Total Submissions: 9150 Accepted: 3050

Description Given m sequences, each contains n non-negative integer. Now we may select one of the sequence to form a sequence with M integers. It's clear that we could get n ^ m this kind of sequences. Then we can calculate the sum of numbers in each sequence, and get n ^ m values. What are we need is the smallest n sums. Could help us?

Input The first line is a integer T, which shows the number of test cases, and then T test cases follow. The first line of each case contains integers m, n (0 < M <=, 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 was separated by a space.

Sample Input

1
2 3
1 2 3
2 2 3

Sample Output

3 3 4

Source POJ Monthly,guang Lin


Test instructions

Give you a M (0<m<=100) sequence containing n (0<n<=2000) positive integers. Now each sequence selects a number and makes up a new sequence, which obviously has a n^m sequence. Now we need to calculate the sum of the series of each sequence of the n^m sequence, and the sum of the first n smallest sequences.

#include <iostream> #include <cstdio> #include <queue> using namespace std;
int a[2100];
    int main () {int t,n,m;
    scanf ("%d", &t);
       while (t--) {int x; Priority_queue<int, Vector<int>, greater<int> >q;//from small to large sort priority_queue<int, vector<int&
       gt;, less<int> >p;//from large to small scanf ("%d%d", &m,&n);
       for (int i=0;i<n;i++) {scanf ("%d", &x);//The first number of rows is stored in order from small to large q.push (x); } for (int i=1;i<m;i++)//traversal after a few lines {for (int j=0;j<n;j++) {scanf ("%d"
              , &a[j]);//Enter the number of each line} while (!q.empty ()) {int sum=q.top (); Q.pop ();//emptying Q for (int j=0;j<n;j++) {if (P.size () <n)//First all the numbers are added with the smallest

                  A sum {p.push (a[j]+sum);//from Big to small, don't forget the number in the back Q queue. ELSE if (P.size () ==n&&a[j]+sum<p.top ())//The P queue is updated to kick away the large number, there is a smaller number added in, this is why to use from the big to the small queue, {                           P.pop ();
                The first n number has been saved with the smallest sum, guaranteed to add a smaller number in the back than the top of the stack P.push (a[j]+sum);//So all the smallest numbers come in.}
                 }} while (!p.empty ()) {Q.push () (P.top ());//From small to a lot of plus and after the number into the queue
              P.pop ();
           }} for (int i=0;i<n;i++) {if (i==n-1) {printf ("%d\n", Q.top ());
           Q.pop ();}
            else {printf ("%d", q.top ());
           Q.pop ();
}}} 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.