[๋ฐฑ์ค€] ๋ณด์„ ๋„๋‘‘ / 1202๋ฒˆ / ํŒŒ์ด์ฌ / Python / heapq/ ํž™ํ
Algorithm/Baekjoon

[๋ฐฑ์ค€] ๋ณด์„ ๋„๋‘‘ / 1202๋ฒˆ / ํŒŒ์ด์ฌ / Python / heapq/ ํž™ํ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’กsolutions 

๐Ÿ’ฌ heapq๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์†Œํž™, ์ตœ๋Œ€ํž™ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Œ.

๐Ÿ’ฌ ๋ณด์„๋“ค์€ ๋ฌด๊ฒŒ๊ฐ€ ์ž‘์€ ์ˆœ์„œ๋Œ€๋กœ ๋ฝ‘ํž ์ˆ˜ ์žˆ๋„๋ก ์ตœ์†Œํž™์œผ๋กœ ๊ตฌํ˜„

๐Ÿ’ฌ ๊ฐ€๋ฐฉ์€ ์ œํ•œ๋œ ๋ฌด๊ฒŒ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ€๋ฒผ์šด ๊ฒƒ๋ถ€ํ„ฐ ๋ฌด๊ฑฐ์šด ์ˆœ์œผ๋กœ ๋‚˜์˜ค๋„๋ก ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

๐Ÿ’ฌ for ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ์ž‘์€ ๋ฌด๊ฒŒ ์ œํ•œ์„ ๊ฐ€์ง„ ๊ฐ€๋ฐฉ๋ถ€ํ„ฐ ๊บผ๋‚ด์™€์„œ ์ œํ•œ๋œ ๋ฌด๊ฒŒ๋ณด๋‹ค ๊ฐ€๋ฒผ์šด ๋ณด์„๋“ค์„ ์ž„์‹œ ๋ฆฌ์ŠคํŠธ์ธ tmp์— ๋ชจ๋‘ ๋‹ด์•„๋‘”๋‹ค -> ์ด๋•Œ ์ฃผ์˜ํ•  ์ ์€ ์ถ”ํ›„ tmp์—์„œ ๊ฐ€์žฅ ๋น„์‹ผ ์• ๋“ค๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๋ฝ‘์„ ์ˆ˜ ์žˆ๊ฒŒ ๊ฐ€๊ฒฉ์— ๋งˆ์ด๋„ˆ์Šค ๋ถ€ํ˜ธ๋ฅผ ๋ถ™์—ฌ tmp ๋ฆฌ์ŠคํŠธ์— heappush๋กœ ๋„ฃ์–ด์ค€๋‹ค. (์ด๊ฒŒ ๋ฐ”๋กœ ์ตœ๋Œ€ํž™ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•)

๐Ÿ’ฌ ๊ฐ€๋ฐฉ์— ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ณด์„ ์ค‘ ๊ฐ€์žฅ ์ œ์ผ ๋น„์‹ผ ๊ฐ€๊ฒฉ์˜ ๋ณด์„์„ ๋ฝ‘์•„์„œ answer์—์„œ ๋นผ์ค€๋‹ค -> ์• ์ดˆ์— ๋ณด์„ ๊ฐ€๊ฒฉ์„ ์Œ์ˆ˜๋กœ ๋„ฃ์–ด๋†จ๊ธฐ ๋•Œ๋ฌธ์— answer์—์„œ ๋นผ์•ผ์ง€ ๋”ํ•˜๋Š” ๊ฐœ๋…์ด ๋œ๋‹ค.

 



๐Ÿ“˜ heap์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž


ํž™(heap)์ด๋ž€?
ํž™์€ ํŠน์ •ํ•œ ๊ทœ์น™์„ ๊ฐ€์ง€๋Š” ํŠธ๋ฆฌ๋กœ, 
์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ๋Š” ์—ฐ์‚ฐ์„ ๋น ๋ฅด๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด
๊ณ ์•ˆ๋œ ์™„์ „์ด์ง„ํŠธ๋ฆฌ๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ

ํž™์—๋Š” ๋‘ ๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋Š”๋ฐ,

์ตœ์†Œ ํž™(Min Heap) : ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ํ‚ค๊ฐ’์ด ์ž์‹ ๋…ธ๋“œ์˜ ํ‚ค๊ฐ’๋ณด๋‹ค ํ•ญ์ƒ ์ž‘์€ ํž™
์ตœ๋Œ€ ํž™(Max Heap) : ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ํ‚ค๊ฐ’์ด ์ž์‹ ๋…ธ๋“œ์˜ ํ‚ค๊ฐ’๋ณด๋‹ค ํ•ญ์ƒ ํฐ ํž™



์ตœ์†Œํž™๊ณผ ์ตœ๋Œ€ํž™

๐Ÿ‘จ‍๐Ÿ’ปcode 

import sys
import heapq
input = sys.stdin.readline
n, k = map(int, input().split())
jewelry = []
for i in range(n):
    weight, price = map(int, input().rstrip().split())
    heapq.heappush(jewelry, [weight, price])
bags = [int(input()) for _ in range(k)]
bags.sort()   # ๊ฐ€๋ฒผ์šด ๊ฒƒ๋ถ€ํ„ฐ ๋ฌด๊ฑฐ์šด ์ˆœ์œผ๋กœ ์ •๋ ฌ

answer = 0
tmp = []
for bag in bags:
    while jewelry and bag >= jewelry[0][0]:   # ๊ฐ€๋ฐฉ์— ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฌด๊ฒŒ๋ณด๋‹ค ๊ฐ€๋ฒผ์šด ๋ณด์„๋“ค์„ ๋ชจ๋‘ ๋‹ด๊ธฐ
        heapq.heappush(tmp, -heapq.heappop(jewelry)[1])
    if tmp:
        answer -= heapq.heappop(tmp)
    elif not jewelry:
        break
print(answer)

 

 

๐Ÿ“Œdescription 

 

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

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

www.acmicpc.net


๋ฌธ์ œ

์„ธ๊ณ„์ ์ธ ๋„๋‘‘ ์ƒ๋•์ด๋Š” ๋ณด์„์ ์„ ํ„ธ๊ธฐ๋กœ ๊ฒฐ์‹ฌํ–ˆ๋‹ค.

์ƒ๋•์ด๊ฐ€ ํ„ธ ๋ณด์„์ ์—๋Š” ๋ณด์„์ด ์ด N๊ฐœ ์žˆ๋‹ค. ๊ฐ ๋ณด์„์€ ๋ฌด๊ฒŒ Mi์™€ ๊ฐ€๊ฒฉ Vi๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ƒ๋•์ด๋Š” ๊ฐ€๋ฐฉ์„ K๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๊ฐ ๊ฐ€๋ฐฉ์— ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฌด๊ฒŒ๋Š” Ci์ด๋‹ค. ๊ฐ€๋ฐฉ์—๋Š” ์ตœ๋Œ€ ํ•œ ๊ฐœ์˜ ๋ณด์„๋งŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

์ƒ๋•์ด๊ฐ€ ํ›”์น  ์ˆ˜ ์žˆ๋Š” ๋ณด์„์˜ ์ตœ๋Œ€ ๊ฐ€๊ฒฉ์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N๊ณผ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N, K ≤ 300,000)

๋‹ค์Œ N๊ฐœ ์ค„์—๋Š” ๊ฐ ๋ณด์„์˜ ์ •๋ณด Mi์™€ Vi๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (0 ≤ Mi, Vi ≤ 1,000,000)

๋‹ค์Œ K๊ฐœ ์ค„์—๋Š” ๊ฐ€๋ฐฉ์— ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฌด๊ฒŒ Ci๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ Ci ≤ 100,000,000)

๋ชจ๋“  ์ˆซ์ž๋Š” ์–‘์˜ ์ •์ˆ˜์ด๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ƒ๋•์ด๊ฐ€ ํ›”์น  ์ˆ˜ ์žˆ๋Š” ๋ณด์„ ๊ฐ€๊ฒฉ์˜ ํ•ฉ์˜ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.




๋ฐ˜์‘ํ˜•