Minimum n sums (use of STL heap)

Source: Internet
Author: User

Minimum n sums (use of STL heap)

Minimum n sum
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 109 (26 users) Total Accepted: 22 (19 users) Rating: Special Judge: No
Description

Given the numbers of Series A and Series B, each of which contains n numbers, the sum of any number from A and B will be obtained, and there will be n ^ 2 results (including repeated ), evaluate the first n smallest sum of n ^ 2 results.


Input

There are multiple groups of test data.

For each group of test data, the first action is n, the second action is A, and the third action is B.

1 <= n <= 100000, 0 <= Ai, Bi <= 10 ^ 9.

Output

For each group of test data, output a row containing the first n smallest sums, which are output in ascending order, separated by a space.

Sample Input
51 3 4 2 07 3 5 2 111074 50 47 45 38 64 19 2 84 6991 46 44 7 67 1 40 60 78 41
Sample Output
2 3 3 4 43 9 20 26 39 42 43 45 46 46
Source
HCPC2014 training competition 3

#include 
 
  #include 
  
   #include 
   
    #include #include 
    
     using namespace std;int a[100005];int b[100005];int sum[100005];int main(){#ifdef xxz    freopen("in.txt","r",stdin);#endif // xxz   int n;    while(cin>>n)    {        for(int i = 0; i < n; i++) cin>>a[i];        for(int i = 0; i < n; i++) cin>>b[i];        sort(a,a+n);        sort(b,b+n);        for(int i = 0; i < n; i++) sum[i] = a[0] + b[i];        make_heap(sum,sum+n);        for(int i =1 ; i < n; i++)        {            for(int j = 0; j < n; j++)            {                int t = b[j] + a[i];                if(t > sum[0]) break;                else                {                    pop_heap(sum,sum+n);                    sum[n-1] = t;                    push_heap(sum,sum+n);                }            }        }        sort(sum,sum+n);        for(int i = 0; i < n-1; i++)        {            cout<
     <<" "; } cout<<
      

<<">

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.