We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 35000ec commit eeadc02Copy full SHA for eeadc02
minjeong/BinarySearch/2024-11-02-[백준]-#2805-나무자르기.py
@@ -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