-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path100000616-01.cpp
71 lines (63 loc) · 1.36 KB
/
100000616-01.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
#include <cstdio>
#include <algorithm>
#define MAXN 100005
using namespace std;
// a max heap
int heap[MAXN], size;
int a[MAXN], b[MAXN];
void down_adjust(int low, int high)
{
int node = low, max_child = node * 2;
if (max_child > high)
return;
if (max_child + 1 <= high && heap[max_child+1] > heap[max_child])
max_child++;
if (heap[max_child] > heap[node])
{
swap(heap[max_child], heap[node]);
down_adjust(max_child, high);
}
}
void create_heap()
{
for (int i = size / 2; i >= 1; i--)
down_adjust(i, size);
}
void heap_sort()
{
int n = size;
while (n)
{
swap(heap[1], heap[n]);
down_adjust(1, --n);
}
}
int main()
{
scanf("%d", &size);
for (int i = 1; i <= size; i++)
scanf("%d", &a[i]);
for (int j = 1; j <= size; j++)
scanf("%d", &b[j]);
// init
for (int i = 1; i <= size; i++)
heap[i] = a[i] + b[1];
create_heap();
// get min-n
for (int i = 1; i <= size; i++)
for (int j = 2; j <= size; j++)
{
if (a[i] + b[j] < heap[1])
{
heap[1] = a[i] + b[j];
down_adjust(1, size);
}
else
break;
}
heap_sort();
for (int i = 1; i <= size; i++)
printf("%d ", heap[i]);
printf("\n");
return 0;
}