From 9de4b068133283d1fd75d4d2caecf93ea278a5ce Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Thu, 24 Jul 2025 21:47:16 +0900 Subject: [PATCH] 20250724 baekjoon --- code_study/Baekjoon/c/1927.c | 133 +++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 code_study/Baekjoon/c/1927.c diff --git a/code_study/Baekjoon/c/1927.c b/code_study/Baekjoon/c/1927.c new file mode 100644 index 0000000..5e10d73 --- /dev/null +++ b/code_study/Baekjoon/c/1927.c @@ -0,0 +1,133 @@ +#include + +void swap(int *x1, int *x2){ + int temp = *x1; + *x1 = *x2; + *x2 = temp; +} + +void insert_heap(int* heap, int *heap_size, int x){ + heap[++(*heap_size)] = x; + int child = *heap_size; + int parent = child / 2; + while(child > 1 && heap[child] < heap[parent]){ + swap(&heap[child], &heap[parent]); + child = parent; + parent = child / 2; + } +} + +int delete_heap(int* heap, int *heap_size){ + if(*heap_size == 0) return 0; + int return_value = heap[1]; + heap[1] = heap[(*heap_size)--]; + int parent = 1; + int child = parent*2; + while(child <= *heap_size){ + if(child + 1 <= *heap_size && heap[child] > heap[child+1]) child++; + if(heap[parent] <= heap[child]) break; + swap(&heap[child], &heap[parent]); + parent = child; + child = parent*2; + } + return return_value; +} + +int main(){ + int heap[100001]; + int heap_size = 0; + + int N; + scanf("%d",&N); + while(N--){ + int input; + scanf("%d",&input); + if(input) insert_heap(heap, &heap_size, input); + else printf("%d\n",delete_heap(heap, &heap_size)); + } + + return 0; +} + +// 9 +// 0 +// 12345678 +// 1 +// 2 +// 0 +// 0 +// 0 +// 0 +// 32 + +// #include +// #include +// #define Max_size 100000 + +// typedef struct{ +// int key; +// }element; + +// typedef struct{ +// element heap[Max_size]; +// int heap_size; +// }HeapType; + +// HeapType* create(){ +// return (HeapType*)malloc(sizeof(HeapType)); +// } + +// void init(HeapType* h){ +// h->heap_size = 0; +// } + +// void insert_heap(HeapType* h, element item){ +// int i = ++(h->heap_size); +// while ((i != 1) && (item.key < h->heap[i / 2].key)) { +// h->heap[i] = h->heap[i / 2]; +// i /= 2; +// } +// h->heap[i] = item; +// } + +// element delete_heap(HeapType* h){ +// int parent, child; +// element item, temp; +// item = h->heap[1]; +// temp = h->heap[(h->heap_size)--]; +// parent = 1; +// child = 2; +// while (child <= h->heap_size) { +// if ((child < h->heap_size) && +// (h->heap[child].key) > h->heap[child + 1].key) +// child++; +// if (temp.key <= h->heap[child].key) break; +// h->heap[parent] = h->heap[child]; +// parent = child; +// child *= 2; +// } +// h->heap[parent] = temp; +// return item; +// } + +// int main() { +// HeapType* h; +// h = create(); +// init(h); +// int N; +// scanf("%d",&N); +// while(N--){ +// int input; +// scanf("%d",&input); +// if(input==0){ +// printf("%d\n",h->heap_size ? delete_heap(h).key : 0); +// } +// else { +// element item; +// item.key = input; +// insert_heap(h, item); +// } +// } +// free(h); +// return 0; +// } \ No newline at end of file