Post

[SWEA] 2930. ํž™(python)

๐Ÿ“Œ๋ฌธ์ œ

Alt text Alt text

๐Ÿ’ช์•„์ด๋””์–ด

  1. max heap๊ตฌํ˜„
    max heap์€ ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋…ธ๋“œ๋“ค๋ณด๋‹ค ํฐ ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ์ด๋‹ค. ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ๋Š” ๋งˆ์ง€๋ง‰ ํŠธ๋ฆฌ์˜ height๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋…ธ๋“œ๊ฐ€ ๋ชจ๋‘ ์กด์žฌํ•˜๊ณ  ๋งˆ์ง€๋ง‰ height์—๋Š” ์™ผ์ชฝ๋ถ€ํ„ฐ ๋…ธ๋“œ๊ฐ€ ์ฐจ์žˆ์œผ๋ฉฐ leaf๋…ธ๋“œ๋Š” ๋งˆ์ง€๋ง‰ height์—๋งŒ ์กด์žฌํ•œ๋‹ค.
    • add
      max heap์— ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์— ์ถ”๊ฐ€๋œ๋‹ค. ์ถ”๊ฐ€ํ•œ ์ธ๋ฑ์Šค์™€ ๋ถ€๋ชจ๋…ธ๋“œ์™€ ๋น„๊ตํ•ด์„œ ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ๋” ํฌ๋ฉด max heap์ƒํƒœ๋ผ ํŒ๋‹จํ•˜๊ณ  heapify๋ฅผ ๋ฉˆ์ถ˜๋‹ค. ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ๋” ์ž‘์œผ๋ฉด ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ์•„๋ž˜๋กœ ๋‚ด๋ฆฌ๊ณ  ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ ๋ถ€๋ชจ ๋…ธ๋“œ ์ž๋ฆฌ๋กœ ์˜ฎ๊ธด ํ›„ (๋ถ€๋ชจ๋…ธ๋“œ๊ฐ€ ๋” ํด๋–„ or root)๊นŒ์ง€ heapify๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.

    • delete
      max heap์— ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•˜๋ฉด root์— ์žˆ๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ root๋…ธ๋“œ ์ž๋ฆฌ๋กœ ์˜ฎ๊ธด๋‹ค.
    • heapify
      ๋ฃจํŠธ๋…ธ๋“œ์—์„œ๋ถ€ํ„ฐ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๋ณด๋‹ค ์ž‘์œผ๋ฉด ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ์ž์‹๋…ธ๋“œ ์ค‘ ๋” ํฐ ์ž์‹๋…ธ๋“œ ์ž๋ฆฌ๋กœ ์˜ฎ๊ธด๋‹ค. ์ž์‹๋…ธ๋“œ๋ณด๋‹ค ํด๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—†์„ ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ ์ž์‹๋…ธ๋“œ์˜ ์—ฌ๋ถ€๋„ ์ฒดํฌํ•ด์•ผํ•œ๋‹ค.

๐Ÿฅ‚์ฝ”๋“œ

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
class max_heap:
    def __init__(self):
        self.queue=[]

    def parent(self,index):
        return (index-1)//2
    
    def left_child(self,index):
        return index*2+1
    
    def right_child(self,index):
        return index*2+2
    
    def swap_node(self,inx,parent):
        self.queue[inx],self.queue[parent]=self.queue[parent],self.queue[inx]

    def add_node(self,node):
        self.queue.append(node)
        last = len(self.queue)-1
        while last>=0:
            parent = self.parent(last)
            if  parent>=0 and self.queue[parent]<self.queue[last]:
                self.swap_node(last,parent)
                last = parent
            else: break

    def delete_node(self):
        last = len(self.queue)-1
        if last<0: 
            return '-1 '
        self.swap_node(last,0)
        max_val = self.queue.pop()
        self.max_heapify(0)
        return '{} '.format(max_val)
    
    def max_heapify(self,inx):
        left,right = self.left_child(inx),self.right_child(inx)
        max_inx = inx
        
        if left<len(self.queue) and self.queue[left]>self.queue[max_inx]:
            max_inx=left
        if right<len(self.queue) and self.queue[right]>self.queue[max_inx]:
            max_inx=right
        
        if max_inx!=inx:
            self.swap_node(inx,max_inx)
            self.max_heapify(max_inx)

T=int(input())
answer=[]
for tc in range(1,T+1):
    tmp='#{} '.format(tc)
    n=int(input())
    heap = max_heap()
    for _ in range(n):
        cal = list(map(int,input().split()))
        if cal[0]==1:
            heap.add_node(cal[1])
        else:
            tmp+=heap.delete_node()
    answer.append(str(tmp))

for a in answer:
    print(a)
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.