오히려 좋아..

상황이 나쁘게만 흘러가는 것 같을 때 외쳐보자.. .

궁금한 마음으로 포트폴리오 보기

Algorithm/백준 온라인 저지(BOJ)

[BOJ] 나무자르기 - 이분탐색

junha6316 2021. 2. 16. 07:57

import sys
sys.setrecursionlimit(100000) 
N, M = map(int, input().split())
woods = list(map(int, input().split()))


def calcHeight(cr, woods):
    answer =0
    for wood in woods:
        if wood > cr:
            answer += wood-cr
    return answer

def bs(height, woods, start, end):
    mid = (start + end)//2
    cutWoods = calcHeight(mid, woods)
    
    if start > end:
        return mid

    if height <= cutWoods:
        start = mid +1
        answer = bs(height, woods, start, end)

    if height > cutWoods:
        end = mid -1
        answer = bs(height, woods, start, end)

    return answer
        
start =1
end = max(woods)
answer = bs(M,woods, start, end)
print(answer)