Skip to content

Commit eeadc02

Browse files
committed
[BOJ] #2805. 나무 자르기 / 실버2 / 38분 / 성공
1 parent 35000ec commit eeadc02

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 나무의 수 N, 가져갈 나무의 길이 M
5+
N, M = map(int, input().split())
6+
# 나무의 길이들 입력
7+
trees = list(map(int, input().split()))
8+
9+
# 시작점과 끝점 설정
10+
left = 0
11+
right = max(trees)
12+
answer = 0
13+
14+
while left <= right:
15+
total = 0
16+
mid = (left + right) // 2
17+
# mid 높이로 나무를 잘라서 얻을 수 있는 나무의 길이 계산
18+
for tree in trees:
19+
# 나무의 높이가 더 크다면
20+
if tree > mid:
21+
total += tree - mid
22+
# 잘린 나무의 길이가 부족하다면 더 낮은 높이(왼쪽 부분) 탐색
23+
if total < M:
24+
right = mid - 1
25+
# 나무 길이가 충분하다면 answer에 저장하고, 더 높게 잘라(오른쪽 부분) 탐색
26+
else:
27+
answer = mid
28+
left = mid + 1
29+
30+
# 최종 절단기 높이 출력
31+
print(answer)

0 commit comments

Comments
 (0)