본문 바로가기
Study/Algorithm & Data structure

[백준][binary search] 나무자르기 python (200930)

by 후이 (hui) 2020. 9. 30.
728x90
반응형

1.  문제 설명 

 

 

 

2. 풀이 

N, M = map(int, input().split(' '))
arr = list(map(int, input().split(' ')))
arr = sorted(arr)

high = arr[-1]
low = 0
def summation(arr, cut):
    res = 0
    for tree in arr:
        if cut > tree:
            continue
        res += tree-cut
    return res

while True:
    mid = (low+high)//2
    res = summation(arr, mid)
    if res == M:
        print(mid)
        exit()
    if high-1 <= low:
        break
    elif res > M:
        low = mid
    else:
        high = mid
print(mid)

 

728x90
반응형

댓글