UVA 11997 K Smallest Sums

Source: Internet
Author: User
Tags arrays ini

Original question:
You ' re given k-arrays, each with a k integers. There is k-K ways to pick exactly one element with each array and calculate the sum of the integers. Your task is to find the K smallest sums among them.
There'll be several test cases. The first line of each case is contains an integer k (2≤k≤750). Each of the following K lines contains k positive integers in each array. Each of the these integers does not exceed 1,000,000. The input is terminated by End-of-file (EOF).
For each test case, print the k smallest sums, in ascending order.
Sample Input
1 8 5
9 2 5
10 7 6
1 1
1 2
Sample Output
9 10 12
2 2

Give you the K number, each array has a number of K. Now ask you each array to take a number plus sum together, then the smallest of the first k is how much.

#include <bits/stdc++.h> using namespace std;
int k;
Vector<int> V[751],ans;
    struct item {int s,b;
    Item (int s,int b): s (s), B (b) {} BOOL operator < (const item &AMP;RHS) Const {return s>rhs.s;

void Ini () {for (int i=1;i<=k;i++) v[i].clear ();}
    void Merge (vector<int> tmp) {priority_queue<item> PQ;
    for (int i=0;i<ans.size (); i++) Pq.push (item (ans[i]+tmp[0],0));
        for (int i=0;i<k;i++) {item t=pq.top ();
        Pq.pop ();
        int b=t.b;
    if (b+1<k) Pq.push (item (t.s-tmp[b]+tmp[b+1],b+1));
    }} int main () {Ios::sync_with_stdio (false);
        while (cin>>k) {ini ();
                for (int i=1;i<=k;i++) {for (int j=1;j<=k;j++) {int res;
            V[i].push_back (RES);
        } sort (V[i].begin (), V[i].end ());} Ans=v[1];
        for (int i=2;i<=k;i++) Merge (V[i]);
            for (int i=0;i<ans.size (); i++) if (I!=ans.size ()-1) cout<<ans[i]<< "";
    else cout<<ans[i]<<endl;
} return 0;

The example in the training guide thought for a while without thinking and looking directly at the answer. The idea is ingenious. The
assumes that there are two arrays before K and Min are saved in ans, and now there is a third array, then the minimum value of the three array is the first k value of the current save plus the K values in the third array. The
maintains the first k minimum values with a priority queue, and then uses the recursive idea to calculate the number of the first and second values in the third array at a time.

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.