2269:minval (priority queue heap sort)

Source: Internet
Author: User

2269:minval time limit: 3 Sec Memory limit:
Submissions: 638 Resolution: 65
Submitted State [Discussion Version] [Propositional person: external import] Title Description

There are two sequences A and b of length n, and the sum of each number in A and B can be obtained N2 and, the N2 and the smallest n.

Input

The first line enters a positive integer N (1<=n<=100000);

The second row n integer AI and ai<=109; the third row n integer bi and bi<=109.

Output

Output only one row, containing n integers, from small to large output this n smallest and, adjacent numbers separated by a space.

Sample input
51 3 2 4 56 3 4 1 7
Sample output
2 3 4) 4 5
/*Heap Sort (priority queue) * Maintain n minimum number * Don't forget to sort*/#include<queue>#include<iostream>#include<cstdio>#include<algorithm>using namespacestd;#defineMAXN 110000intN;intNUM1[MAXN], NUM2[MAXN]; Priority_queue<int>Min_num;intRESULT[MAXN];intMain () {CIN>>N;  for(intI=1; I<=n; i++) {scanf ("%d", &Num1[i]); }     for(intj=1; J<=n; J + +) {scanf ("%d", &Num2[j]); } sort (Num1+1, num1+n+1) ; Sort (num2+1, num2+1+N);  for(intI=1; I<=n; i++) {Min_num.push (num1[1] +Num2[i]); }     for(intI=2; I<=n; i++){         for(intj=1; J<=n; J + +){            if(Num1[i] + num2[j] <Min_num.top ())                 {Min_num.pop (); Min_num.push (Num1[i]+Num2[j]); }Else {                 Break ; }        }    }     for(intI=1; I<=n; i++) {Result[i]=Min_num.top ();     Min_num.pop (); }     for(intI=n; i>=1; i--) {printf ("%d", Result[i]); }    return 0 ; }

2269:minval (priority queue heap sort)

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.