Skip to content

Commit 6e4da29

Browse files
authored
Merge pull request #136 from AlgorithmStudy-Allumbus/minjeong
2์›” 1์ฃผ์ฐจ ๋ฐœ์ œ ์ž๋ฃŒ ์—…๋กœ๋“œ
2 parents 556126e + 5691b7c commit 6e4da29

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'''
2+
BOJ #19638. ์„ผํ‹ฐ์™€ ๋งˆ๋ฒ•์˜ ๋ฟ…๋ง์น˜ (์‹ค๋ฒ„1)
3+
https://www.acmicpc.net/problem/19638
4+
์œ ํ˜•: Priority Queue, Data Structure
5+
'''
6+
7+
# ๊ณผ์ œ ๋ฌธ์ œ์˜ ๋‹ต์€ PR ์˜ฌ๋ฆฌ๋Š” ๋‚ ์— ํ•จ๊ป˜ ์˜ฌ๋ผ๊ฐˆ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## ๐Ÿš€2์›” 1์ฃผ์ฐจ (2/3) ์Šคํ„ฐ๋”” ๋ฐœ์ œ ์ฃผ์ œ: Dynamic Programming - Tree
2+
> ๋ฐœ์ œ์ž: ๊น€๋ฏผ์ • (@Mingguriguri)
3+
4+
> ์ฃผ์ œ: Priority Queue
5+
### ๐Ÿ—‚๏ธ ์Šคํ„ฐ๋”” ์ž๋ฃŒ
6+
- PDF: [๋ฐ”๋กœ๊ฐ€๊ธฐ
7+
](./Study_BOJ_1202.pdf)
8+
9+
<img width="500" alt="์Šคํ„ฐ๋””๋ฌธ์ œ" src="https://github.com/user-attachments/assets/96863997-1cfd-454b-b1b0-634606cc4108" /> <img width="500" alt="๋ฐœ์ œ๋ฌธ์ œ" src="https://github.com/user-attachments/assets/10717940-e761-4a5c-8cf0-e75935d84633" />
10+
11+
### ๐Ÿ“– ๋ฌธ์ œ
12+
- [๋ฐฑ์ค€ #1202. ๋ณด์„ ๋„๋‘‘](https://www.acmicpc.net/problem/1202): Priority Queue, ์ž๋ฃŒ๊ตฌ์กฐ / ๊ณจ๋“œ2
13+
- ์ •๋‹ต ์ฝ”๋“œ: [Study_BOJ_1202_๋ณด์„๋„๋‘‘.py](./Study_BOJ_1202_๋ณด์„๋„๋‘‘.py)
14+
15+
### ๐Ÿ’ป ๊ณผ์ œ
16+
- [๋ฐฑ์ค€ #19638. ์„ผํ‹ฐ์™€ ๋งˆ๋ฒ•์˜ ๋ฟ…๋ง์น˜](https://www.acmicpc.net/problem/19638): Priority Queue, ์ž๋ฃŒ๊ตฌ์กฐ / ์‹ค๋ฒ„1
17+
- ์ •๋‹ต ์ฝ”๋“œ: [Assignment_BOJ_19638_์„ผํ‹ฐ์™€๋งˆ๋ฒ•์˜๋ฟ…๋ง์น˜.py](./Assignment_BOJ_19638_์„ผํ‹ฐ์™€๋งˆ๋ฒ•์˜๋ฟ…๋ง์น˜.py)
Binary file not shown.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
BOJ #1202. ๋ณด์„ ๋„๋‘‘(๊ณจ๋“œ2)
3+
https://www.acmicpc.net/problem/1202
4+
์œ ํ˜•: Priority Queue, Data Structure, Greedy
5+
'''
6+
import sys
7+
import heapq
8+
9+
input = sys.stdin.readline
10+
11+
# Step 1: ์ž…๋ ฅ ๋ฐ›๊ธฐ
12+
N, K = map(int, input().split()) # N: ๋ณด์„ ๊ฐœ์ˆ˜, K: ๊ฐ€๋ฐฉ ๊ฐœ์ˆ˜
13+
jewelry = [] # (๋ฌด๊ฒŒ, ๊ฐ€๊ฒฉ)
14+
bags = [] # ๊ฐ€๋ฐฉ์˜ ์ตœ๋Œ€ ๋ฌด๊ฒŒ
15+
16+
for _ in range(N):
17+
M, V = map(int, input().split())
18+
jewelry.append((M, V))
19+
20+
for _ in range(K):
21+
bags.append(int(input()))
22+
23+
# Step 2: ๋ณด์„๊ณผ ๊ฐ€๋ฐฉ ์ •๋ ฌ
24+
jewelry.sort() # ๋ณด์„์„ ๋ฌด๊ฒŒ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
25+
bags.sort() # ๊ฐ€๋ฐฉ์„ ๋ฌด๊ฒŒ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
26+
27+
# Step 3: ์šฐ์„ ์ˆœ์œ„ ํ (์ตœ๋Œ€ ํž™) ์‚ฌ์šฉ
28+
max_heap = []
29+
result = 0
30+
idx = 0
31+
32+
# ๊ฐ€๋ฐฉ์„ ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌ
33+
for bag in bags:
34+
# ํ˜„์žฌ ๊ฐ€๋ฐฉ์ด ์ˆ˜์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ณด์„์„ ๋ชจ๋‘ ์ถ”๊ฐ€ (๋ฌด๊ฒŒ ๊ธฐ์ค€ ์ •๋ ฌ๋œ ์ƒํƒœ)
35+
while idx < N and jewelry[idx][0] <= bag:
36+
heapq.heappush(max_heap, -jewelry[idx][1]) # ์ตœ๋Œ€ ํž™์„ ์œ„ํ•ด ์Œ์ˆ˜ ์ €์žฅ
37+
idx += 1
38+
39+
# ๊ฐ€์žฅ ๊ฐ€์น˜๊ฐ€ ๋†’์€ ๋ณด์„์„ ์„ ํƒ
40+
if max_heap:
41+
result += -heapq.heappop(max_heap)
42+
43+
print(result)
44+

0 commit comments

Comments
ย (0)