HLG2062 (make,heap problem)

Source: Internet
Author: User

The smallest N and
Time limit:1000 MS Memory limit:32768 K
Total submit:129(Notoginseng users) Total accepted:35(users) Rating: Special Judge: No
Description

Given a, B two series, each containing n number, respectively, from A and B to take a number of the addition of the sum, so there will be n^2 results (including repetition), n^2 results of the first n the smallest sum.

Input

There are multiple sets of test data.

For each set of test data, the first behavior is N, the second behavior sequence A, and the third Act Series B.

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

Output

For each set of test data, the output line contains the first n minimum and the output in ascending order, separated by a space of two.

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


Code

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace Std;
int a[100005],b[100005],sum[100005];
int main ()
{
int n;
while (scanf ("%d", &n)!=eof)
{
for (int i=0;i<n;i++)
scanf ("%d", &a[i]);
Sort (a,a+n);
for (int i=0;i<n;i++)
scanf ("%d", &b[i]);
Sort (b,b+n);
for (int i=0;i<n;i++)
SUM[I]=B[I]+A[0];
Make_heap (sum,sum+n);//Build a maximum heap
for (int i=1;i<n;i++)
for (int j=0;j<n;j++)
{
int TEMP=A[I]+B[J];
if (Temp>=sum[0])
Break
Pop_heap (sum,sum+n)/////swap the root element with the last element and reorder the n-1 elements again
Sum[n-1]=temp;
Push_heap (Sum,sum+n),////////To heap the heap and make the maximum heap sort

}

Sort (sum,sum+n);//////The first n number is ordered from the largest heap from small to large, so that the following output
for (int i=0;i<n-1;i++)
printf ("%d", sum[i]);
printf ("%d\n", sum[n-1]);
}
return 0;
}

HLG2062 (make,heap problem)

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.