λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸƒ algorithm/boj

BOJ 18111 - λ§ˆμΈν¬λž˜ν”„νŠΈ

by HandHand 2021. 3. 8.

문제

λ°±μ€€ 온라인 저지 - 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

πŸ’¬ λŒ“κΈ€