UVA 11997 K Smallest sums (priority queue)

Source: Internet
Author: User

UVA 11997 K Smallest sums (priority queue)

The following:

Give you an integer k, and give you K group number, each group of K number, now in each group to take a number, and then add can get a "sum", such and a total of k^k. You want to output all of the possible and lowest K.

Analysis:

Liu Rugia: Training guide P189 examples.

Question 1: If there are only a,b,c three arrays of size k, how do we ask "and" to get the smallest first k and then?

All we need to do is to get a and b arrays out of the first K small and (the first k+1 of all and the values after the small to all of us do not have to, because these values are used in the back), and then use this and the array to the same and C arrays to find the first K small and If there are k such arrays, we still use A1 and A2 to find the K-small sum, and then with the array and A3 summation, and then continue with the result of the same and A4 sum, and A5 sum ... And so on.

Question 2: A and B two arrays seek the K-small and how to calculate the solution quickly.

The complexity of the method of k^2. Here we use the precedence queue to solve. Because there were k^2 possible and results.

Assuming that the numbers in A and B have been sorted (from small to large), then the number of the first k will certainly come out from the following K queues ( we equate the k^2 and the K queues, and the elements in each queue are from small to large ):

Queue 1:a[1]+b[1], a[1]+b[2], ..., a[1]+b[k]

Queue 2:a[2]+b[1], a[2]+b[2], ..., a[2]+b[k]

Queue 3:a[3]+b[1], a[3]+b[2], ..., a[3]+b[k]

Queue 4:a[4]+b[1], a[4]+b[2], ..., a[4]+b[k]

...

Queue 4:a[k]+b[1], a[k]+b[2], ..., a[k]+b[k]

And the first element of each queue squadron is certainly the current smallest candidate and one. So each time we find the smallest of the K-team first element in the K queue as a front K small and (each operation complexity for LOGK), the continuous K-operation can get all the former K small and.

( Note: The program implementation uses only one priority queue priority_queue, which holds the K-first element of the K-queue above )

So we only need k^logk complexity to find the first k decimal of the 2 array combinations.

To sum up, we only need to build two array of first k decimal to get the smallest first k decimal.

AC Code (new):

#include <cstdio> #include <algorithm> #include <queue> using namespace std;
const int MAXN = 750+5;

int A[MAXN][MAXN]; Node used to build precedence queues element struct node {int sum;//and int id;//b array element subscript node (int sum,int ID): Sum (sum), ID (ID) {} bool
    operator< (const Node &AMP;RHS) Const {return sum>rhs.sum;

}
};
    A and B array of the first n small and stored in the C array void merge (int *a,int *b,int *c,int n) {priority_queue<node> Q;

    for (int i=0;i<n;i++) Q.push (Node (a[i]+b[0],0)); for (int i=0;i<n;i++) {Node tmp=q.top ();
        Q.pop ();
        C[i]=tmp.sum;
    if (tmp.id+1<n) Q.push (Node (tmp.sum-b[tmp.id]+b[tmp.id+1), tmp.id+1);
    int main () {int k;
                while (scanf ("%d", &k) ==1) {for (int i=0;i<k;i++) {for (int j=0;j<k;j++)
            scanf ("%d", &a[i][j]);
        Sort (a[i],a[i]+k);

        for (int i=1;i<k;i++) merge (a[0],a[i],a[0],k); printf ("%d",A[0][0]);
        for (int i=1;i<k;i++) printf ("%d", a[0][i]);
    printf ("\ n");
return 0; }

AC Code:

#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std;
const int maxn=1000;
int A[MAXN],B[MAXN];
int k; struct Node {int va;//a array value int id;//b array element ordinal bool operator< (const NODE&AMP;RS) const {return
    va+b[id]>rs.va+b[rs.id];
}
};
    void merge (int *a,int *b,int *c) {priority_queue<node> PQ;
        for (int i=0;i<k;i++) {node R;
        R.va=a[i];
        R.id=0;
    Pq.push (R);
        for (int i=0;i<k;i++) {node r=pq.top ();p q.pop ();
        C[i]=r.va+b[r.id];
            if (r.id<k-1) {r.id++;
        Pq.push (R); int main () {while (scanf ("%d", &k) ==1) {for (int i=0;i<k;i++) scanf ("%d",&
        A[i]);
        Sort (a,a+k);
            for (int i=1;i<k;i++) {for (int j=0;j<k;j++) scanf ("%d", &b[j]);
            Sort (b,b+k); Merge (A,b,a);
        printf ("%d", a[0]);
        for (int i=1;i<k;i++) printf ("%d", a[i]);
    printf ("\ n");
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.