λ¬Έμ
λ°±μ€ μ¨λΌμΈ μ μ§ - 18111λ²
νμ΄ κ³Όμ
격μ ννμ λ
μ΄ μ£Όμ΄μ§κ³ λμ΄λ₯Ό λ§μΆκΈ° μν μ΅μ μκ°κ³Ό κ·Έλμ λμ΄λ₯Ό ꡬνλ λ¬Έμ μ
λλ€.
μ²μμλ μ΄λΆ νμ
μ μ΄μ©ν΄μ μ΅μ μκ°μ ꡬνλ €κ³ νμ§λ§ λμΌν μκ°μ λμ΄κ° μ΅λμΈ κ²½μ°λ₯Ό μ°Ύλ κ³Όμ μ΄
λͺ
ννμ§ μμμ κ·Έλ₯ μμ νμ
μΌλ‘ ν΄κ²°νλ κ²μΌλ‘ λ°©ν₯μ μ‘μμ΅λλ€.
μ΄ 0 ~ 256
μ λμ΄λ₯Ό νμνλ©΄ λλλ° μ΄λ 격μλ₯Ό O(N^2)
μΌλ‘ νμνλ©΄ μκ° μ΄κ³Όκ° λ°μν©λλ€.
λμ μ μ²λ¦¬ κ³Όμ μ ν΅ν΄ κ° κ²©μμ λμ΄ κ°μ μ μ₯νκ³ κΈ°μ€ λμ΄λ³΄λ€ μκ±°λ ν° κ°μλ₯Ό μΈμ΄ νμν μκ° κ°μ κ³μ°νλ©΄ λ©λλ€.
μ½λ
import sys
N, M, B = list(map(int, sys.stdin.readline().split()))
board = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def pre_processing():
heights = {}
for x in range(N):
for y in range(M):
height = board[x][y]
if height in heights:
heights[height] += 1
else:
heights[height] = 1
return heights
def calc_groundwork(dist, height):
remove, addon = 0, 0
for h, cnt in dist.items():
if h > height:
remove += (h - height) * cnt
elif h < height:
addon += (height - h) * cnt
inventory = B + remove
if inventory < addon:
return sys.maxsize
else:
time = remove * 2 + addon
return time
def solution():
dist = pre_processing()
ans_time, ans_height = sys.maxsize, 0
for height in range(257):
time = calc_groundwork(dist, height)
if time <= ans_time:
ans_time = time
ans_height = height
print(f"{ans_time} {ans_height}")
solution()
λ°μν
'π algorithm > boj' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
BOJ 13905 - μΈλΆ (0) | 2021.03.08 |
---|---|
BOJ 2458 - ν€ μμ (0) | 2021.03.08 |
BOJ 1806 - λΆλΆν© (0) | 2021.03.08 |
BOJ 6186 - Best Grass (0) | 2021.03.08 |
BOJ 8972 - λ―ΈμΉ μλμ΄λ Έ (0) | 2021.03.08 |
π¬ λκΈ