2805 나무자르기

김가영·2020년 11월 18일
0

Algorithm

목록 보기
22/78
post-thumbnail

문제로 가기

import sys
input = sys.stdin.readline

c_tree, target = list(map(int, list(input().split())))
trees = list(map(int, list(input().split())))
trees.sort(reverse=True)
def check(cutHeight):
    result = 0
    for i in trees:
        if i > cutHeight:
            result += i - cutHeight
            if target <= result:
                return True
        else:
            break
    return False

start = 0
end = trees[0]

def search(start, end):
    if end <= start:
        return start
    if (start + end )//2 == (start+ end)/2:
        mid = (start + end )//2
    else:
        mid = (start+ end) //2 + 1
    if check(mid):
        start = mid
    else:
        end = mid - 1
    return search(start, end)

print(search(start, end))

reversion

def search():
    start, end = 0, trees[0]
    ans = 0
    while start <= end:
        mid = (start + end) // 2
        if check(mid):
            ans = mid
            start = mid + 1
        else:
            end = mid - 1
    return ans
profile
개발블로그

0개의 댓글