๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 1781 - ์ปต๋ผ๋ฉด

by HandHand 2021. 6. 7.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 1781๋ฒˆ

ํ’€์ด ๊ณผ์ •

๋งˆ๊ฐ์ผ์„ ๊ณ ๋ คํ•˜์—ฌ ๊ฐ€์žฅ ๋งŽ์€ ์ปต๋ผ๋ฉด์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ๋Š” ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๋ฐ๋“œ๋ผ์ธ์˜ ๋งˆ์ง€๋ง‰ ๋‚ ๋ถ€ํ„ฐ ์ˆœํšŒ๋ฅผ ํ•˜๋ฉด์„œ ํ•ด๋‹นํ•˜๋Š” ๋‚  ์ด์ƒ์˜ ๋‚ ์งœ์— ๋Œ€ํ•ด์„œ ๊ฐ€์žฅ ๋งŽ์€ ์ปต๋ผ๋ฉด์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ์„ ํƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด ์šฐ์„  ์ˆœ์œ„ ํ ๋ฅผ ๋‘ ๊ฐœ ์‚ฌ์šฉํ•ด์„œ ํ•˜๋‚˜๋Š” ๋ฐ๋“œ๋ผ์ธ์˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ, ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ์ปต๋ผ๋ฉด์˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys
import heapq


N = int(input())
questions = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]


def init_queue():
    pq = []
    for deadline, cups in questions:
        heapq.heappush(pq, [-deadline, -cups])
    return pq


def solution():
    pq = init_queue()
    day = -pq[0][0]
    answer = 0

    candidates = []
    while day > 0:
        while pq and -pq[0][0] >= day:
            target = heapq.heappop(pq)
            heapq.heappush(candidates, target[1])

        if candidates:
            select = -heapq.heappop(candidates)
            answer += select
        day -= 1

    return answer


print(solution())
๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ๋Œ“๊ธ€