반응형
문제 보기
2805번: 나무 자르기
문제 상근이는 나무 M미터가 필요하다. 근처에 나무를 구입할 곳이 모두 망해버렸기 때문에, 정부에 벌목 허가를 요청했다. 정부는 상근이네 집 근처의 나무 한 줄에 대한 벌목 허가를 내주었고, 상근이는 새로 구입한 목재절단기을 이용해서 나무를 구할것이다. 목재절단기는 다음과 같이 동작한다. 먼저, 상근이는 절단기에 높이 H를 지정해야 한다. 높이를 지정하면 톱날이 땅으로부터 H미터 위로 올라간다. 그 다음, 한 줄에 연속해있는 나무를 모두 절단해버린다. 따
www.acmicpc.net
파이썬(Python) 풀이
N, M = map(int, input().split())
trees_height = list(map(int, input().split()))
min_height = 0; max_height = max(trees_height)
while max_height > min_height + 1:
cutting_height = (max_height + min_height)//2
sum = 0
for trees in trees_height:
if cutting_height < trees:
sum += (trees - cutting_height)
if sum >= M:
min_height = cutting_height
else:
max_height = cutting_height
if max_height == min_height + 1:
cutting_height = min_height
sum = 0
for trees in trees_height:
if cutting_height < trees:
sum += (trees - cutting_height)
print(cutting_height)
Python3로 제출하면 시간 초과가 났고, PyPy3로 제출하니 통과했습니다.
반응형
'DEVLOG > Algorithms' 카테고리의 다른 글
[파이썬(Python)] 회전행렬 / 2차원배열 회전하는 법 구현하기 (0) | 2019.09.19 |
---|---|
[알고리즘/코딩테스트] 2019 NAVER 신입 공채 준비하기 (0) | 2019.09.18 |
[이분 탐색] 알고리즘 설명 및 예제 풀이 (0) | 2019.09.10 |
[BOJ 1157] 단어 공부 - 파이썬 풀이 (0) | 2019.09.10 |
[BOJ 10809] 알파벳 찾기 - 파이썬 풀이 (0) | 2019.09.10 |
댓글