# UV 11997 the K smallest sums

Source: Internet
Author: User

A matrix of K * k is given, and each row takes a number to form the sum of K numbers. There are K ^ K possibilities in total, and the first K is the smallest.

I made a mistake at the beginning, because as long as I ordered each line, the smallest must be the sum of the first column. Then I thought about it and gradually promoted it, change the number of rows to the number of columns in the lower column each time. Of course, the spacing is the minimum. Obviously, this is not the case. In this way, each number is used only once, and the meaning of the question is obviously repeated multiple times.

Then we can see it as two rows, that is, processing two rows at a time, leaving the minimum K rows to continue processing with the row read next time. This method is quite powerful, but there is a difficulty: how can we quickly get the first K in the two rows? I tried to calculate it all over again, k ^ K .. Timeout .. After thinking about it for a long time, I didn't come up with a more effective method. Later I saw a very embarrassing method, and it was easy to prove that

The first row of a  to a [K-1] respectively and the second row of B  constitute the earliest K number, at present, I only know that a  + B  is the smallest, and the rest are not sure, but I can be sure that the second is small, it must be a  + B  or one of the above number of K-1, that is to say, I put these numbers into the priority queue, and then each pop out the smallest number, output, then push the next of the smallest number into the queue, then the next Pop must be the second smallest... In this way, repeat K times to obtain the first K small numbers.

`#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;struct node{    int s,b;    bool operator < (const node& rhs) const{        return s>rhs.s;    }};priority_queue<node> q;const int N=800;int n;int A[N];int B[N];void solve(){    while (!q.empty()) q.pop();    for (int i=0;i<n;i++){        q.push((node){A[i]+B,0});    }    int cnt=0;    while (cnt<n){        node tmp=q.top();        q.pop();        A[cnt++]=tmp.s;        q.push((node){tmp.s-B[tmp.b]+B[tmp.b+1],tmp.b+1});    }}int main(){    while (scanf("%d",&n)!=EOF)    {        while (!q.empty()) q.pop();        for (int i=0;i<n;i++){            scanf("%d",&A[i]);        }        for (int i=1;i<n;i++){            for (int j=0;j<n;j++){                scanf("%d",&B[j]);            }            sort(B,B+n);            solve();        }        printf("%d",A);        for (int i=1;i<n;i++){            printf(" %d",A[i]);        }        puts("");    }    return 0;}`

Related Keywords:

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.