티스토리 뷰

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
100
101
102
103
104
105
106
107
108
 
#include <stdio.h>
 
#define MAX_SIZE 100
 
int heap[MAX_SIZE];
int heapSize = 0;
 
void heapInit(void)
{
    heapSize = 0;
}
 
int heapPush(int value)
{
    if (heapSize + 1 > MAX_SIZE)
    {
        printf("queue is full!");
        return 0;
    }
    
    heap[heapSize] = value;
    
    int current = heapSize;
    while (current > 0 && heap[current] > heap[(current - 1/ 2])
    {
        int temp = heap[(current - 1/ 2];
        heap[(current - 1/ 2= heap[current];
        heap[current] = temp;
        current = (current - 1/ 2;
    }
    
    heapSize = heapSize + 1;
    
    return 1;
}
 
int heapPop(int *value)
{
    if (heapSize <= 0)
    {
        return -1;
    }
    
    *value = heap[0];
    heapSize = heapSize - 1;
    
    heap[0= heap[heapSize];
    
    int current = 0;
    while (current * 2 + 1 < heapSize)
    {
        int child;
        if (current * 2 + 2 == heapSize)
        {
            child = current * 2 + 1;
        }
        else
        {
            child = heap[current * 2 + 1> heap[current * 2 + 2] ? current * 2 + 1 : current * 2 + 2;
        }
        
        if (heap[current] > heap[child])
        {
            break;
        }
        
        int temp = heap[current];
        heap[current] = heap[child];
        heap[child] = temp;
        
        current = child;
    }
    return 1;
}
 
int main(int argc, char* argv[])
{
    int T, N;
    
    scanf("%d"&T);
    
    for (int test_case = 1; test_case <= T; test_case++)
    {
        scanf("%d"&N);
        
        heapInit();
        
        for (int i = 0; i < N; i++)
        {
            int value;
            scanf("%d"&value);
            heapPush(value);
        }
        
        printf("#%d ", test_case);
        
        for (int i = 0; i < N; i++)
        {
            int value;
            heapPop(&value);
            printf("%d ", value);
        }
        printf("\n");
    }
    return 0;
}
 
cs

댓글
공지사항
최근에 달린 댓글
Total
Today
Yesterday
«   2025/02   »
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
링크
글 보관함