"UVA 11997 K smallest Sums" priority queue

Source: Internet
Author: User

Examples from the priority queue of the training guide.

Title Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18702

Test instructions: Given a k array of integers, each containing k elements. Taking an element together in each array, you can get the KK and the smallest k values (not the weight).

Data range: K [2, 750]

Idea: Violent enumeration of k^k is undesirable.

"First look at the simplified version of the problem: give two K-length arrays A and B, respectively, in A and B to take a number and add, you can get k^2 and, to find these and the smallest K." ”

First, A and B are sorted from small to large. Because only the smallest of the first k values, we can first assume that b[0] and a k elements of a set of elements are the smallest sum of k, and then each time the minimum value of s smin saved to the result array R, and the Smin minus B[i], plus b[i+1] after the resulting value is put back into the set S, Keep the size of s always K. The K-elements in R are the result after the K-wheel is removed-and the operation is put back. We called this process a merge of a-B. The complexity of merging once is O (KLOGK)

s requires K-times insertions and deletions, with a minimum value, so the data structure chooses the priority queue implemented by the complete binary heap.

Now from the two array, a, B to the K-group A1~ak, simply consider the A1 as a, followed by the i:2~k, each time the AI merged into the A1, so after the K 22 merge, the number of K in A1 is the result. Total complexity O (K2LOGK)

1#include <cstdio>2#include <algorithm>3#include <queue>4 using namespacestd;5 Const intMax_k =760;6 7 structelem{8     intsum, b;9Elem (intSintb): sum (s), B (b) {}Tenelem&operator= (Constelem&e) { Onesum =e.sum; Ab =e.b; -         return* This; -     } the     BOOL operator< (Constelem& e)Const{ -         returnSum > e.sum;//Small Top Pile -     } - }; +  - voidMergeintAintBintN) { +Priority_queue<elem>PQ; A      for(intI=0; i<n; i++){ atPq.push (Elem (a[i] + b[0],0)); -     } -      for(intI=0; i<n; i++){ -Elem e =pq.top (); -A[i] =e.sum; - Pq.pop (); inE.sum = e.sum-b[e.b] + b[e.b+1]; -e.b = e.b +1; to Pq.push (e); +     } - //for (int i=0; i<n; i++) { the //printf ("%d", A[i]); * //    } $ //printf ("\ n");Panax Notoginseng } -  the intK; + intA[max_k], b[max_k]; A  the intMain () + { -Freopen ("11997.txt","R", stdin); $      while(~SCANF ("%d", &k)) { $          for(intI=0; i<k; i++){ -scanf"%d", &a[i]); -         } theSort (A, A +k); -          for(intI=1; i<k; i++){Wuyi              for(intj=0; j<k; J + +){ thescanf"%d", &b[j]); -             } WuSort (b, + +k); - merge (A, b, k); About         } $printf"%d", a[0]); -          for(intI=1; i<k; i++){ -printf"%d", A[i]); -         } Aprintf"\ n"); +     } the     return 0; -}

"UVA 11997 K smallest Sums" priority queue

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.