목차

나무 자르기

ps
링크acmicpc.net/…
출처BOJ
문제 번호2805
문제명나무 자르기
레벨실버 3
분류

파라메트릭 서치

시간복잡도O(nlogn)
인풋사이즈n <= 1,000,000
사용한 언어Python
제출기록146524KB / 860ms
최고기록440ms
해결날짜2021/07/18
태그

21단계

풀이

코드

코드 1 - 파라메트릭 서치 (2052ms)

"""Solution code for "BOJ 2805. 나무 자르기".

- Problem link: https://www.acmicpc.net/problem/2805
- Solution link: http://www.teferi.net/ps/problems/boj/2805
"""

from teflib import algorithm


def main():
    def is_less_than_m(height):
        total = 0
        for t in trees:
            if t <= height:
                return True
            total += t - height
            if total >= M:
                return False
        return True

    N, M = [int(x) for x in input().split()]
    trees = [int(x) for x in input().split()]

    trees.sort(reverse=True)
    answer = algorithm.binary_search(0, max(trees) + 2, is_less_than_m) - 1

    print(answer)


if __name__ == '__main__':
    main()

코드 2 - 정렬 (860ms)

"""Solution code for "BOJ 2805. 나무 자르기".

- Problem link: https://www.acmicpc.net/problem/2805
- Solution link: http://www.teferi.net/ps/problems/boj/2805
"""


def main():
    N, M = [int(x) for x in input().split()]
    trees = [int(x) for x in input().split()]

    trees.sort(reverse=True)
    trees.append(0)
    total_cut = 0
    for i in range(1, N + 1):
        total_cut += (trees[i - 1] - trees[i]) * i
        if total_cut >= M:
            answer = trees[i] + (total_cut - M) // i
            break

    print(answer)


if __name__ == '__main__':
    main()