-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_sequence.cpp
75 lines (66 loc) · 1.46 KB
/
heap_sequence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* =====================================================================================
*
* Filename: heap_sequence.cpp
*
* Description: The N largest of N * N
*
* Version: 1.0
* Created: 07/04/14 21:19:38
* Revision: none
* Compiler: gcc
*
* Author: (),
* Company: NDSL UESTC
*
* =====================================================================================
*/
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN = 100;
int N;
int A[MAXN], B[MAXN];
typedef struct node_t{
int sum;
int a;
int b;
bool operator>(const node_t &other) const {
return sum > other.sum;
}
}node_t;
void k_merge() {
sort(A, A + N);
sort(B, B + N);
priority_queue<node_t, vector<node_t>, greater<node_t> > q;
for ( int i = 0; i < N; ++i ) {
node_t node;
node.sum = A[0] + B[i];
node.a = 0;
node.b = i;
q.push(node);
}
for ( int i = 0; i < N; ++i ) {
node_t node = q.top(); q.pop();
cout << node.sum << endl;
node_t node1;
node1.sum = A[node.a + 1] + B[node.b];
node1.a = node.a + 1;
node1.b = node.b;
q.push(node1);
}
}
int main() {
cin >> N;
int tmp;
for (int i = 0; i < N; ++i ) {
cin >> A[i];
}
for (int i = 0; i < N; ++i ) {
cin >> B[i];
}
k_merge();
return 0;
}