-
Notifications
You must be signed in to change notification settings - Fork 1
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
15-alstjr7437 #53
Conversation
heap = [] | ||
|
||
for i in map(int, input().split()): | ||
heappush(heap, i) |
There was a problem hiding this comment.
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())))
μ΄λ κ²λ λꡬμ.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μν μ¬μ€ heapμ λ€μ 곡λΆνλ©΄μ heapifyλ₯Ό λ΄€μλλ°
κ·Έλ₯ νκ΅¬μ‘°λ‘ λ°κΏμ£Όλλ° listλ₯Ό λ§λ€μ΄λκ³ λ€μ heapμΌλ‘ λ°κΎΈλ©΄ μκ° λ¬Έμ κ° μκΈΈκΉλ΄ κ·Έλ¬μ΅λλ€!!
νΉμ λ°λ‘ μκ°μ κ΄λ ¨λ λΆλΆμ μμκΉμ?
There was a problem hiding this comment.
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κΉμ§μ κ³Όμ μ
λλ€.
λ λ€ λ¨μΌ μμμ λν΄μ μ°μ°μ μνν κ²½μ° μκ°λ³΅μ‘λλ
κ·Έλ¦¬κ³ νμ΄μ¬μ heapifyλ Sift down λ°©μμΌλ‘ ꡬνλμ΄ μμ΅λλ€. λ°λΌμ heappushλ‘ μΌμΌμ΄ μμλ₯Ό μ§μ΄λ£λ κ²λ³΄λ€, νλμ 리μ€νΈλ₯Ό heapifyλ₯Ό ν΅ν΄ νμΌλ‘ λ§λλ κ²μ΄ λ ν¨μ¨μ μ λλ€.
Build Heap μκ°λ³΅μ‘λ μ΄ν΄νκΈ°, Heap μλ£κ΅¬μ‘° μ΄ κ²μκΈμ μ°Έκ³ ν΄λ³΄μΈμ.
temp = 0 | ||
temp += heappop(heap) + heappop(heap) |
There was a problem hiding this comment.
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)
μ λ°λ‘ ν λΉ μνμ ¨μ£
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΄λ¨Έ.. μ€μλ₯Ό..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€ γ γ μ΄κ±° dequeλ‘ μ λ ¬λλ €μ μΌμͺ½μμ λκ° λ½μλ λλ €λ? νκ³
from collections import deque
import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
N = int(input())
files = deque(map(int, input().split()))
cost = 0
while len(files) > 1:
files = deque(sorted(files))
file1 = files.popleft()
file2 = files.popleft()
cur_cost = file1 + file2
cost += cur_cost
files.append(cur_cost)
print(cost)
μ΄λ κ² ν΄λ΄€λλ° μμ μκ°μ΄κ³Όκ° λ¨λ€μ. νμ μμλ€!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ²μμ μ€ν¨νκΈΈλ μκ·Έλ°κ° νλλ λ²μ 1,000,000 μ΄ 10000 κ° λν΄μ§λ©΄ 10μ΅μ΄λΌ λ²μλ₯Ό λμ΄κ°λλΌκ΅¬μ. κ·Έλμ Long μΌλ‘ λ°κΏ¨λλ ν΅κ³Όνμ¨λ€.
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.PriorityQueue
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val t = br.readLine().toInt()
repeat(t) {
val n = br.readLine().toInt()
val line = br.readLine().split(" ").map { it.toLong() }
val pq = PriorityQueue<Long>().apply { addAll(line) }
var cost: Long = 0L
// νμΌμ΄ νλκ° λλ©΄ λ
while (pq.size > 1) {
val sum = pq.poll() + pq.poll()
cost += sum
pq.add(sum)
}
println(cost)
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ°μ μμ ν μ°λ λ² μ λ³Ό μ μμ΄μ μ’μλ κ±° κ°μμ! λ―Όμ λ μ½λ λ³΄κ³ μ°Έκ³ ν΄μ λ€μ ν λ² νμ΄λ΄€μ΅λλΉ~
import sys
from heapq import *
input = sys.stdin.readline
T = int(input())
for _ in range(T):
N = int(input())
chapters = list(map(int, input().split()))
time_list = []
min_time=0
for chapter in chapters :
heappush(time_list, chapter)
while len(time_list) > 1 :
temp =0
temp += heappop(time_list) + heappop(time_list)
heappush(time_list, temp)
min_time += temp
print(min_time)
π λ¬Έμ λ§ν¬
νμΌ ν©μΉκΈ°3
βοΈ μμλ μκ°
30λΆ
π‘ λ¬Έμ μ΄ν΄
μΌλ¨ λ¬Έμ λ₯Ό μ΄ν΄νμλ©΄
κ·Έλμ λ¬Έμ κ°
40 30 30 50 μ΄ λ€μ΄μ€λ©΄
40 + 30 = 70 -> 70 30 50
70 + 30 = 100 -> 100 50
100 + 50 = 150 -> 150
70 + 100 + 150 = 320μΌλ‘ λ©λλ€.
κ·Έ μ€ μ μΌ μμ μ΄ν©μ κ³μ°νλκ±°λκΉ
μμ λΆλΆμ κ·Έλ₯ 2κ°λ₯Ό λνκ³ νλμ© μΆκ°ν΄μ λνλ κ²½μ°μκ³ μ μΌ μμ κ²½μ°μ μλ‘ κ°λ €λ©΄
μκ°ν λΆλΆμ΄ μ λ ¬μ ν νμ μμ μλ₯Ό 2κ°μ© λνλ©΄ μ μΌ μμ κ²½μ°κ° λμ΅λλ€.
30 30 40 50
30 + 30 = 60 -> 60 40 50
40 + 50 = 90 -> 60 90
60 + 90 = 150 -> 150
60 + 90 + 150 = 300
β¨ μλ μ½λ
μμ κ°μ΄ μκ°νκ³ κ·Έλμ λμ¨ μλ μ½λλ
3-1. 3μ λμ¨ μλ₯Ό κ²°κ³Όμ λνλ€. (κ²°κ³Ό(c)λ₯Ό μ΄ν©(answer)μ λ£κΈ°
3-2. 3μ λμ¨ μλ₯Ό λ€μ heapμ pushνλ€.(30 + 30 = 60) 60μ heapμ λ£κΈ° -> 60, 40, 50
μ½λ
π μλ‘κ² μκ²λ λ΄μ©
heapμ κΉλ¨Ήμμλλ° μ€λλ§μ λ€μ μ°λ €λκΉ κ°λ μ λ€μ 곡λΆνλ©΄μ νλλ° μ’μμ΅λλ€!