ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

1655๋ฒˆ: ๊ฐ€์šด๋ฐ๋ฅผ ๋งํ•ด์š”

์ฒซ์งธ ์ค„์—๋Š” ์ˆ˜๋นˆ์ด๊ฐ€ ์™ธ์น˜๋Š” ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๊ทธ ๋‹ค์Œ N์ค„์— ๊ฑธ์ณ์„œ ์ˆ˜๋นˆ์ด๊ฐ€ ์™ธ์น˜๋Š” ์ •์ˆ˜๊ฐ€ ์ฐจ๋ก€๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ์ •์ˆ˜๋Š” -1

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 N๊ฐœ์˜ ์ˆ˜๊ฐ€ ์ฐจ๋ก€๋Œ€๋กœ ์ž…๋ ฅ๋  ๋•Œ, ์ค‘๊ฐ„ ๊ฐ’์„ ์ถœ๋ ฅํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” `heap`์„ left, right๋กœ ๋‚˜๋ˆ„๊ณ , left์˜ ๋ฃจํŠธ๊ฐ€ ์ค‘๊ฐ„๊ฐ’์ด ๋˜๋„๋ก ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. left๋Š” ์ตœ๋Œ€ ํž™์œผ๋กœ ๋งŒ๋“ค๊ณ , right๋Š” ์ตœ์†Œ ํž™์œผ๋กœ ๋งŒ๋“ค๋ฉด pop์—ฐ์‚ฐ์„ ํ™œ์šฉํ•˜๊ธฐ ์ข‹๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
import heapq


if __name__ == '__main__':
    n = int(stdin.readline())
    left, right = [], []

    for _ in range(n):
        num = int(stdin.readline())

        if len(left) == len(right):
            heapq.heappush(left, -num)
        else:
            heapq.heappush(right, num)

        if right and -left[0] > right[0]:
            heapq.heappush(left, -heapq.heappop(right))
            heapq.heappush(right, -heapq.heappop(left))

        print(-left[0])

 

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€