๋ฐฑ์ค€ 1927๋ฒˆ๊ณผ ํŒŒ์ด์ฌ ํž™

2021. 3. 26. 11:06ใ†Python

๋ฐฑ์ค€ 1927๋ฒˆ ๋ฌธ์ œ๋Š”:

https://www.acmicpc.net/problem/1927

 

1927๋ฒˆ: ์ตœ์†Œ ํž™

์ฒซ์งธ ์ค„์— ์—ฐ์‚ฐ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ์—ฐ์‚ฐ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ x๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋งŒ์•ฝ x๊ฐ€ ์ž์—ฐ์ˆ˜๋ผ๋ฉด ๋ฐฐ์—ด์— x๋ผ๋Š” ๊ฐ’์„ ๋„ฃ๋Š”(์ถ”๊ฐ€ํ•˜๋Š”) ์—ฐ์‚ฐ์ด๊ณ , x๊ฐ€ 0

www.acmicpc.net

์ „๊ณต์—์„œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋ฐฐ์šธ ๋•Œ ํž™์„ C๋กœ ๊ตฌํ˜„ํ•ด์„œ ์ฝ”๋“œ๋„ ๊ธธ๊ณ ... ์งœ๋‹ค๋ณด๋‹ˆ ์ง‘์ค‘๋ ฅ์ด ๋–จ์–ด์ ธ์„œ ์‹ค์ˆ˜๋ฅผ ๋งŽ์ดํ–ˆ๋˜ ๊ธฐ์–ต์ด ์žˆ๋‹ค. 

๊ทธ๋Ÿฐ ํž™์„ ํŒŒ์ด์ฌ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๋ญ”๊ฐ€ heap์ด๋ผ๋Š”๊ฒŒ ์กด์žฌํ•  ๊ฒƒ ๊ฐ™์•„์„œ ์ฐพ์•„๋ณด๋‹ˆ ์‹ค์ œ๋กœ

import heapq

๋กœ ํž™์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํŒŒ์ด์ฌ์— ๋ชจ๋“ ๊ฒŒ ์ค€๋น„๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฑธ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค. ์•ผํ˜ธ

 

ํž™ ํ•จ์ˆ˜๋“ค

1. heapq.heappush

heap = [] #๋น„์–ด ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ

#heapq.heappush(๋ฆฌ์ŠคํŠธ, ๋„ฃ๊ณ  ์‹ถ์€ ์ˆซ์ž)
heapq.heappush(heap, 0)
heapq.heappush(heap, 1)

print(heap)

=====================================
<์ฝ”๋“œ run>

[0, 1]

์กฐ๊ธˆ ๋…ํŠนํ•˜๊ฒŒ๋„ heappush๋Š” ์•ž์— ๋‚ด๊ฐ€ ๋จผ์ € ์ƒ์„ฑํ•ด๋‘” ๋ฆฌ์ŠคํŠธ์— ๊ฐ’์„ ๋„ฃ๊ณ , ๊ทธ ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ์•Œ์•„์„œ ์ตœ์†Œ ํž™์˜ ๊ธฐ์ค€์— ๋งž๊ฒŒ 

์•ž์—์„œ๋ถ€ํ„ฐ ๊ฐ’์ด ๊ฐ€์žฅ ์ž‘์€ ์›์†Œ๋ฅผ ์ •๋ ฌํ•ด์„œ ๋ณด์—ฌ์ค€๋‹ค. 

2. heap1.heappop

print(heapq.heappop(heap))

===============================
<์ฝ”๋“œ run>
0

heappop์€ ์ตœ์†Œ ํž™์˜ root, ์ฆ‰ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ returnํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์ƒˆ๋กœ์šด ๊ฐ’์— ์ €์žฅํ•ด์„œ printํ•˜๊ฑฐ๋‚˜ ๊ทธ๋ƒฅ printํ•ด๋„ ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๊ณ , ๊ทธ ๊ฐ’์€ ๋‚ด๊ฐ€ ๋งŒ๋“  heap์ด๋ผ๋Š” list์—์„œ ์‚ฌ๋ผ์ง„๋‹ค.

 

๋ฌธ์ œ ํ•ด๊ฒฐ(1927๋ฒˆ)

์–ด์ฐจํ”ผ ๋ฐฑ์ค€์—๋„ ์†Œ์Šค ๊ณต๊ฐœ๋ฅผ ํ•ด๋‘๋Š” ํŽธ์ด๋ผ ๋ถ€์กฑํ•˜์ง€๋งŒ.. ์†Œ์Šค๋ฅผ ๊ณต๊ฐœํ•ด๋ณด์ž๋ฉด,

 

from sys import stdin
import heapq

def inputZero():
    try:
        print(heapq.heappop(heap))
    except:
        print(0)

def inputElse(num):
    heapq.heappush(heap,num)

heap = []

N = int(stdin.readline())
for _ in range(0,N):
    num = int(stdin.readline())
    if num == 0:
        inputZero()
    else:
        inputElse(num)

์ด๋ ‡๊ฒŒ ํ•ด๊ฒฐํ–ˆ๋‹ค. N์„ ์ž…๋ ฅ ๋ฐ›์•„ for๋ฌธ์„ ๋Œ๋ฆฌ๋ฉฐ ํ•œ ๋ฒˆ์”ฉ ์ž…๋ ฅ ๋ฐ›๊ณ  ๋ฐ”๋กœ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ˜•์‹์œผ๋กœ ๊ตฌ์„ฑํ–ˆ๋‹ค.

(stdin.readline()์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๋Š” ๋ฐฑ์ค€์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ์ž์ฃผ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ..)

 

๋งŒ์•ฝ 0์„ ์ž…๋ ฅ ๋ฐ›์œผ๋ฉด

def inputZero():
    try:
        print(heapq.heappop(heap))
    except:
        print(0)

inputZero()๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š”๋ฐ, ์ด๋•Œ try except ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. ๋งŒ์•ฝ heapop(heap)์—์„œ ์ธ๋ฑ์Šค๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด indexError๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ๊ทธ๋•Œ ๋ฐ”๋กœ except๋กœ ๋„˜์–ด๊ฐ€์„œ 0์„ printํ•ด์ฃผ๋Š” ์‹์ด๋‹ค.

 

def inputElse(num):
    heapq.heappush(heap,num)

๊ทธ๋ฆฌ๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ ์ถ”๊ฐ€ํ•  ์ˆซ์ž๋ฅผ ์ž…๋ ฅ ๋ฐ›์œผ๋ฉด InputElse(num)์ด๋ผ๋Š” ํ•จ์ˆ˜๋กœ pushํ•ด์ค€๋‹ค.

 

์‚ฌ์‹ค C์— ๋น„ํ•˜๋ฉด.. ์ž๋ฃŒ๊ตฌ์กฐ ์ž์ฒด๋ฅผ ์™„๋ฒฝํžˆ ์•Œ๊ณ  ์žˆ์–ด์„œ ์ฝ”๋“œ๋ฅผ ์งค ์ˆ˜ ์žˆ๋‹ค๊ธฐ๋ณด๋‹ค๋Š” ๊ทธ๋ƒฅ ๊ฐœ๋…๋งŒ ์•Œ๊ณ  ๋ญ˜ Import ํ• ์ง€ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ๊ธˆ๋ฐฉ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ฒ˜๋Ÿผ ๋Š๊ปด์ง„๋‹ค. ๋‹ค๋งŒ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๋Š˜ ์•„์‰ฝ๊ณ , C๋กœ ์ƒˆ๋กœ ์งœ๋ฉด์„œ ์ดํ•ด ํ•˜๋Š”๊ฒŒ ์ค‘์š”ํ•  ๋“ฏ ํ•˜๋‹ค.