https://www.acmicpc.net/problem/18111

μ΄ν΄
- νννκ° κ°λ₯ν λμ΄μ λν΄ λͺ¨λ κ²½μ°μ μλ₯Ό λΉκ΅ν΄κ°λ©° νμ΄μΌνλ λ¬Έμ μ΄λ€.
- λ¬Έμ λ₯Ό μ½μ΄λ³΄λ©΄ μ μ μ λ―, λμ΄μ λν μΈλ±μ€κ° μ€μνμ§ μμ 2μ°¨μ λ°°μ΄μ΄ μλ 1μ°¨μ λ°°μ΄λ‘ μ λ ₯μ λ°μλ λκ³ , Dictionary ννλ‘ λ°μλ λλ€.
ꡬν
- N*M νλ ¬μ λμ΄μ λν Dictionaryλ‘ μ μ
- collections λ΄λΆ defaultdictλ₯Ό νμ©
- λμ΄μ λν΄ μμ λΈλ μμ λΆμ λΈλ μλ₯Ό λ°λ‘ μ μ₯νμ¬ κ³μ° μμΌλ‘ νμ΄
- λ§λ€ μ μλκ°? π (λΆμ λΈλ μ) + (μ΅μ΄ μΈλ²€ν 리 λΈλ μ) >= (μλ λΈλ μ)
- 걸리λ μκ° π (λΆμ λΈλ μ) + 2 * (μλ λΈλ μ)
- 걸리λ μ΅μ μκ° μ μ₯ λ° μ΅μ μκ° μ€λ³΅ μ μ΅λ λμ΄ λΉκ΅
μ½λ
# 18111 λ§μΈν¬λννΈ
import sys
from collections import defaultdict
input = sys.stdin.readline
# νννλ₯Ό ν μ μλμ§ μ²΄ν¬
def check(candidate:defaultdict, h:int):
# νλ μ, μλ μ
mine, build = 0, 0
for key, val in candidate.items():
# type Error λλ μ΄μ (key μ€ str ννκ° μλλ΄)
key = int(key)
# λ§μ½ λ§λλ €λ λμ΄κ° μλλΌλ©΄
if key != h:
diff = abs(key - h) * val
# λμ΄ μ°¨μ΄ λ§νΌ λν΄μ€
if key > h:
mine += diff
else:
build += diff
# μμΌλ €λ λΈλ μκ° μΈλ²€ν 리μ μμΌλ©΄ -1, μλλ©΄ κ±Έλ¦° μκ° λ°ν
return -1 if mine+B <build else 2*mine+build
def solution():
# μ΅μ μκ°κ³Ό λμ΄ μ μ₯μ©
min_t,ans =500*500*257+1,-1
# μ΅μ ~ μ΅λκΉλ§ λ리면 μ μ μμ(λ€λ₯Έ κ²½μ°λ μ΅μμκ°μ κ°μ§ μ μμ)
for h in range(minh, maxh+1):
# μκ° κ³μ°
time = check(house, h)
# -1μ΄λ©΄ ν¨μ€
if time >= 0:
# μ΅μ μκ° κ³μ°
if min_t > time:
min_t = time
ans = h
# μ΅μ μκ°μ΄ μ¬λ¬ κ°λ©΄ λμ΄κ° κ°μ₯ λμ κ²
elif min_t == time:
if ans < h:
ans = h
return (min_t, ans)
if __name__ == "__main__":
N, M, B = map(int, input().split())
# dictionary λ‘ μ μ₯νλ©΄ μ΅μ
μ κ²½μ°κ° 500*500 -> 256λ²μΌλ‘ μ€μ
house = defaultdict(int)
minh,maxh = 257, -1
for _ in range(N):
for j in list(map(int, input().split())):
house[j] += 1
minh = min(minh, j)
maxh = max(maxh, j)
print(*solution())
μ°Έμ‘°
Defaultdict?
- Dictionary λ₯Ό λ§λλ dict ν΄λμ€μ μλΈ ν΄λμ€
- Dictionaryμ μ μ¬νμ§λ§ μ΅μ΄ μ μΈν νμ μμ΄ μ¬μ© κ°λ₯
- μ΅μ νΈμΆ μ 미리 μ§μ λ νμ μ κΈ°λ³Έκ°μΌλ‘ μ μ
- μμ
from collections import defaultdict
default_dict = defaultdict(int)
default_dict['key']
print(default_dict)
>> defaultdict(<class 'int'>, {'key': 0})λ°μν
'π STUDY > μλ£κ΅¬μ‘° & μκ³ λ¦¬μ¦' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
| 11286. μ λκ° ν (0) | 2024.03.12 |
|---|---|
| ν¬λ£¨μ€μΉΌ(Kruskal) - μ΅μ μ μ₯ νΈλ¦¬(MST) (1) | 2024.01.10 |
| [μκ³ λ¦¬μ¦] Union-Find (1) | 2024.01.07 |
| 1717. μ§ν©μ νν (2) | 2024.01.07 |
| 13994. μλ‘μ΄ λ²μ€ λ Έμ (0) | 2023.03.06 |