Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15-alstjr7437 #53

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions alstjr7437/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
| 11์ฐจ์‹œ | 2024.02.23 | ํ•ด์‹œ | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/42579">๋ฒ ์ŠคํŠธ ์•จ๋ฒ”</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/39 |
| 12์ฐจ์‹œ | 2024.02.26 | BFS | <a href="https://www.acmicpc.net/problem/3197">๋ฐฑ์กฐ์˜ ํ˜ธ์ˆ˜</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/41 |
| 13์ฐจ์‹œ | 2024.02.29 | DP | <a href="https://www.acmicpc.net/problem/10844">์‰ฌ์šด ๊ณ„๋‹จ ์ˆ˜</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/42 |
| 14์ฐจ์‹œ | 2024.03.03 | ๋ธŒ๋ฃจํŠธํฌ์Šค | <a href="https://www.acmicpc.net/problem/18111">๋งˆ์ธํฌ๋ž˜ํ”„ํŠธ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/48 |
| 15์ฐจ์‹œ | 2024.03.09 | ์šฐ์„ ์ˆœ์œ„ ํ | <a href="https://www.acmicpc.net/problem/13975">ํŒŒ์ผ ํ•ฉ์น˜๊ธฐ3</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/53 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from heapq import *

t = int(input())

for _ in range(t):
n = int(input())
answer = 0
heap = []

for i in map(int, input().split()):
heappush(heap, i)
Comment on lines +8 to +11

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๋ ‡๊ฒŒ ํ•˜๋‚˜์”ฉ ๋„ฃ์„ ํ•„์š” ์—†์ด

heap = list(map(int, input().split()))
heapify(heap)

์ผ๋ฐ˜ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•˜๊ณ  heapify๋ฅผ ์ˆ˜ํ–‰ํ•ด์ฃผ๋ฉด ํž™ ๊ตฌ์กฐ๋กœ ํ•œ ๋ฒˆ์— ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค. ๋” ์ค„์ด์ž๋ฉด

heapify(heap := list(map(int, input().split())))

์ด๋ ‡๊ฒŒ๋„ ๋˜๊ตฌ์š”.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•„ํ•˜ ์‚ฌ์‹ค heap์„ ๋‹ค์‹œ ๊ณต๋ถ€ํ•˜๋ฉด์„œ heapify๋ฅผ ๋ดค์—ˆ๋Š”๋ฐ
๊ทธ๋ƒฅ ํž™๊ตฌ์กฐ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š”๋ฐ list๋ฅผ ๋งŒ๋“ค์–ด๋‘๊ณ  ๋‹ค์‹œ heap์œผ๋กœ ๋ฐ”๊พธ๋ฉด ์‹œ๊ฐ„ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ๊นŒ๋ด ๊ทธ๋žฌ์Šต๋‹ˆ๋‹ค!!
ํ˜น์‹œ ๋”ฐ๋กœ ์‹œ๊ฐ„์— ๊ด€๋ จ๋œ ๋ถ€๋ถ„์€ ์—†์„๊นŒ์š”?

Copy link

@9kyo-hwang 9kyo-hwang Mar 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heap์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐ๋Š” Sift up ๋ฐฉ์‹๊ณผ Sift down ๋ฐฉ์‹์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

sift up ๋ฐฉ์‹์€ heappush ๊ณผ์ •์—์„œ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋กœ, ์ฃผ์–ด์ง„ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ ๋…ธ๋“œ์™€ ๋น„๊ตํ•ด heap property ๋งŒ์กฑ์‹œํ‚ค๊ฑฐ๋‚˜, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด root์— ๋„์ฐฉํ•  ๋•Œ ๊นŒ์ง€ swapํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋œปํ•ฉ๋‹ˆ๋‹ค.
sift down ๋ฐฉ์‹์€ heappop ๊ณผ์ •์—์„œ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ๋กœ, ์ฃผ์–ด์ง„ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ ์ค‘ key๊ฐ€ ํฐ ๊ฐ’(or ์ž‘์€ ๊ฐ’)๊ณผ ๋น„๊ตํ•ด heap property๋ฅผ ๋งŒ์กฑํ•˜๊ฑฐ๋‚˜, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด leaf์— ๋„์ฐฉํ•  ๋•Œ ๊นŒ์ง€ swap ํ•˜๋Š” ๊ณผ์ •์„ ๋œปํ•ฉ๋‹ˆ๋‹ค.

sift up์€ leaf -> root๊นŒ์ง€ ๊ฐ€๋Š” ๊ณผ์ •์ด๊ณ  sift down์€ internal -> leaf๊นŒ์ง€์˜ ๊ณผ์ •์ž…๋‹ˆ๋‹ค.
๋‘˜ ๋‹ค ๋‹จ์ผ ์š”์†Œ์— ๋Œ€ํ•ด์„œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ๊ฒฝ์šฐ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” $O(log N)$์ด๋‚˜, ์™„์ „์ด์ง„ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๊ฐ–๋Š” ํž™์˜ ํŠน์„ฑ๋•Œ๋ฌธ์— ์ „์ฒด ์š”์†Œ์— ๋Œ€ํ•ด heap์„ ๊ตฌ์„ฑํ•˜๋Š” ์‹œ๊ฐ„์€ Sift up์˜ ๊ฒฝ์šฐ $O(NlogN)$์ด๊ณ  Sift down์˜ ๊ฒฝ์šฐ $O(N)$ ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํŒŒ์ด์ฌ์˜ heapify๋Š” Sift down ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ heappush๋กœ ์ผ์ผ์ด ์š”์†Œ๋ฅผ ์ง‘์–ด๋„ฃ๋Š” ๊ฒƒ๋ณด๋‹ค, ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ heapify๋ฅผ ํ†ตํ•ด ํž™์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋” ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.

Build Heap ์‹œ๊ฐ„๋ณต์žก๋„ ์ดํ•ดํ•˜๊ธฐ, Heap ์ž๋ฃŒ๊ตฌ์กฐ ์ด ๊ฒŒ์‹œ๊ธ€์„ ์ฐธ๊ณ ํ•ด๋ณด์„ธ์š”.


while len(heap) > 1 :
temp = 0
temp += heappop(heap) + heappop(heap)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temp = heappop(heap) + heappop(heap)

์™œ ๋ฐ”๋กœ ํ• ๋‹น ์•ˆํ•˜์…จ์ฃ 

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์–ด๋จธ.. ์‹ค์ˆ˜๋ฅผ..

heappush(heap, temp)
answer += temp
print(answer)
Loading