๋ฐฑ์ค€

๋ฐฑ์ค€ 11286 ์ ˆ๋Œ“๊ฐ’ ํž™ - ํŒŒ์ด์ฌ

stoneeee 2022. 11. 19. 18:27

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

 

11286๋ฒˆ: ์ ˆ๋Œ“๊ฐ’ ํž™

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

www.acmicpc.net

 

์ตœ์†Œ ํž™์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

ํž™์— [์ ˆ๋Œ“๊ฐ’, ๋ถ€ํ˜ธ]๋ฅผ ๋„ฃ์–ด๋‘๊ณ  heappopํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ“-ํŒŒ์ด์ฌ์€ comparator ๊ฐ™์€ ๊ฑฐ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„

1. ์ ˆ๋Œ“๊ฐ’์ด ์ž‘์€ ์ˆ˜

2. ์ ˆ๋Œ“๊ฐ’์ด ๊ฐ™์œผ๋ฉด ๋ถ€ํ˜ธ๊ฐ€ ์Œ์ธ ์ˆ˜

์ˆœ์„œ๋Œ€๋กœ popํ•ด์ค€๋‹ค.

 

from sys import stdin
import heapq

heap = []

for _ in range(int(input())):
    x = int(stdin.readline())

    if x == 0:
        if heap:
            val, sgn = heapq.heappop(heap)
            print(val * sgn)
        else:
            print(0)
    else:
        heapq.heappush(heap, [abs(x), x//abs(x)])