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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

1202๋ฒˆ: ๋ณด์„ ๋„๋‘‘

์ฒซ์งธ ์ค„์— N๊ณผ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N, K ≤ 300,000) ๋‹ค์Œ N๊ฐœ ์ค„์—๋Š” ๊ฐ ๋ณด์„์˜ ์ •๋ณด Mi์™€ Vi๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (0 ≤ Mi, Vi ≤ 1,000,000) ๋‹ค์Œ K๊ฐœ ์ค„์—๋Š” ๊ฐ€๋ฐฉ์— ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฌด๊ฒŒ Ci๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ Ci

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๋ณด์„์ ์— ๊ฐ ๋ณด์„์— ๋Œ€ํ•œ ๋ฌด๊ฒŒ์™€ ๊ฐ€๊ฒฉ์ด ์ฃผ์–ด์ง„๋‹ค. ๋˜ํ•œ, K๊ฐœ์˜ ๊ฐ€๋ฐฉ์— ๊ฐ๊ฐ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ฌด๊ฒŒ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋‹ค. ์ด๋•Œ ํ›”์น  ์ˆ˜ ์žˆ๋Š” ๋ณด์„์˜ ์ตœ๋Œ€ ๊ฐ€๊ฒฉ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด ๋ฌธ์ œ๋Š” ํ‰๋ฒ”ํ•œ ๋ฐฐ๋‚ญ๊ณผ ๋‹ฌ๋ฆฌ, heap์„ ์ด์šฉํ•˜์—ฌ ํ‘ธ๋Š” ๋ฌธ์ œ์ด๋‹ค. heap์˜ ์„ฑ์งˆ๊ณผ Python์—์„œ ์‚ฌ์šฉ๋˜๋Š” `heapq`๋ฅผ ์ดํ•ดํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ์–ด๋ ต์ง€ ์•Š๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

  • ๋ณด์„๊ณผ ๊ฐ€๋ฐฉ์„ ๋ฌด๊ฒŒ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
  • ๊ฐ€๋ฐฉ์˜ ๋ฌด๊ฒŒ ์ œํ•œ๋ณด๋‹ค ๋ณด์„์˜ ๋ฌด๊ฒŒ๊ฐ€ ์ž‘๋‹ค๋ฉด, heap์— ๋„ฃ๋Š”๋‹ค.
    • heapify๋ฅผ ํ•  ๋•Œ, ์ตœ๋Œ€ ํž™์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฌด๊ฒŒ๋ฅผ ์Œ์ˆ˜๋กœ ํ•˜์—ฌ root node์˜ ๊ฐ’์ด ์ตœ๋Œ€ ๊ฐ’์ด ๋˜๋„๋ก ํ•œ๋‹ค.
  • ์ฆ‰, ๋ฌด๊ฒŒ ์ œํ•œ์ด ๊ฐ€์žฅ ์ž‘์€ ๊ฐ€๋ฐฉ๋ถ€ํ„ฐ ๋ฌด๊ฒŒ๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๋ณด์„๋ถ€ํ„ฐ ๋“ค์–ด๊ฐ€๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ์ตœ๋Œ€ ๊ฐ’์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
import heapq


if __name__ == "__main__":
    WEIGHT, PRICE = 0, 1
    n, k = map(int, stdin.readline().split())
    gem = sorted([list(map(int, stdin.readline().split())) for _ in range(n)])
    bag = sorted(int(stdin.readline()) for _ in range(k))

    heap = []
    cur_gem = answer = 0

    for weight_limit in bag:
        while cur_gem < n and weight_limit >= gem[cur_gem][WEIGHT]:
            # ์ตœ๋Œ€ ํž™์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์Œ์ˆ˜๋กœ.
            heapq.heappush(heap, -gem[cur_gem][PRICE])
            cur_gem += 1

        if heap:
            answer += -heapq.heappop(heap)

    print(answer)

 

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