-
Notifications
You must be signed in to change notification settings - Fork 1
/
baekjoon_11279_max_heap.c
99 lines (88 loc) · 2.26 KB
/
baekjoon_11279_max_heap.c
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* baekjoon_11279_max_heap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mihykim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/21 10:27:53 by mihykim #+# #+# */
/* Updated: 2020/04/21 10:33:14 by mihykim ### ########.fr */
/* */
/* ************************************************************************** */
/*
** - Write a program that process the operations defined below using Max Heap.
** - Operation
** 1) Insert a natural number into the array.
** 2) Print the max value in the array(0 if empty) and delete it.
** - Do operation 1, if the input number is a natural number.
** Do operation 2, if the input number is zero.
** - Input will be like :
** 5 <= # of operations
** 0 <= Do peration 2
** 123456 <= Do peration 1
** 1
** 0
** 32
*/
#include <stdio.h>
int heap[100000];
int size = 0;
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void insert(int input)
{
int i;
i = ++size;
heap[i] = input;
while (i/2 && heap[i] > heap[i/2])
{
swap(&heap[i/2], &heap[i]);
i = i/2;
}
}
void print_and_delete(void)
{
int i;
int max_child;
if (size == 0)
{
printf("0\n");
return ;
}
printf("%d\n", heap[1]);
swap(&heap[1], &heap[size--]);
i = 1;
while (2 * i <= size)
{
if (2 * i + 1 > size)
max_child = 2 * i;
else
max_child = heap[2 * i] >= heap[2 * i + 1] ? 2 * i : 2 * i + 1;
if (heap[i] >= heap[max_child])
return ;
swap(&heap[i], &heap[max_child]);
i = max_child;
}
}
int main(void)
{
int num_operation;
int input;
scanf("%d", &num_operation);
getchar();
while (num_operation--)
{
scanf("%d", &input);
getchar();
if (input == 0)
print_and_delete();
else
insert(input);
}
return (0);
}