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

BOJ 2805 - λ‚˜λ¬΄ 자λ₯΄κΈ°

by HandHand 2021. 3. 18.

문제

λ°±μ€€ 온라인 저지 - 2805번

풀이 κ³Όμ •

λ‚˜λ¬΄κΎΌμ΄ μ›ν•˜λŠ” 벌λͺ©λŸ‰μ„ λ§Œμ‘±ν•˜λŠ” 졜적의 톱날 μœ„μΉ˜λ₯Ό νƒμƒ‰ν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

λ‹¨μˆœνžˆ 높이λ₯Ό 1μ”© μ¦κ°€μ‹œν‚¨λ‹€λ©΄ κ°’μ˜ λ²”μœ„κ°€ 맀우 λ„“κΈ° λ•Œλ¬Έμ— μ‹œκ°„μ•ˆμ— 탐색이 λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€.
λ”°λΌμ„œ 이뢄 탐색을 톡해 톱날 μœ„μΉ˜λ₯Ό 찾아주도둝 ν•©λ‹ˆλ‹€.
ν†±λ‚ μ˜ μœ„μΉ˜λ₯Ό 높일 수둝 벌λͺ©ν•˜λŠ” 양이 μ€„μ–΄λ“ λ‹€λŠ” 점을 μ΄μš©ν•΄μ„œ 탐색을 μˆ˜ν–‰ν•˜λ©΄ λ©λ‹ˆλ‹€.

μ½”λ“œ


import sys

N, M = list(map(int, sys.stdin.readline().split()))
trees = list(map(int, sys.stdin.readline().split()))


def possible(saw):
    temp = 0
    for tree in trees:
        if tree > saw:
            temp += (tree - saw)
    return True if temp >= M else False


def solution():
    global trees

    trees.sort()

    lo = 0
    hi = trees[-1]

    answer = 0
    while lo <= hi:
        mid = (lo + hi) // 2
        if possible(mid):
            answer = mid
            lo = mid + 1
        else:
            hi = mid - 1

    return answer


print(solution())
λ°˜μ‘ν˜•

'πŸƒ algorithm > boj' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

BOJ 15240 - Paint bucket  (0) 2021.03.18
BOJ 1449 - 수리곡 ν•­μŠΉ  (0) 2021.03.18
BOJ 1654 - λžœμ„  자λ₯΄κΈ°  (0) 2021.03.18
BOJ 3184 - μ–‘  (0) 2021.03.18
BOJ 7562 - λ‚˜μ΄νŠΈμ˜ 이동  (0) 2021.03.18

πŸ’¬ λŒ“κΈ€