Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

28-xxubin04 #108

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions xxubin04/Binary Search/2805_나무 자르기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
input = open(0).readline

tree_num, length = map(int, input().split())
trees = sorted(list(map(int, input().split())))

def binary_search(target, trees):
start = 0
end = trees[-1]

while start <= end:
mid = (start + end) // 2
cutted_trees = 0

for i in trees:
if (i - mid) >= 0:
cutted_trees += (i - mid)
Comment on lines +12 to +16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

채현이 PR에서도 설명한 부분이긴 한데,
이진 탐색 문제가 어려워질 수록 start, end 값을 조정하기 위한 조건 판별 코드가 길어집니다.
보통 이 부분은 check란 이름의 함수로 따로 분리하는 편입니다.

def check(cutter_height):
    sum_remain = 0
    for tree_height in trees:
        if tree_height > height_of_cutter:
            sum_remain += (tree_height - cutter_height)
            
    return sum_remain


if cutted_trees == target:
return mid
elif cutted_trees > target:
start = mid + 1
else:
end = mid - 1

if cutted_trees >= target:
ans = mid
else:
ans = mid - 1
Comment on lines +18 to +28
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위 check를 이용한다고 했을 때,

ans = 0
lo, hi = 1, max(trees)
while lo <= hi:
    mid = (lo + hi) // 2
    
    if check(mid) >= M:  # 자르고 남은 나무 길이 합이 원래 가져가고자 했던 길이보다 많거나 같으면
        ans = mid  # 현재 커팅 지점을 최대 지점으로 저장
        lo = mid + 1  # 하한선 증가
    else:  # 아니라면
        hi = mid - 1  # 상한선 감소
        
print(ans)

이런 식으로 조건을 더 간략하게 할 수 있겠죠?


return ans


print(binary_search(length, trees))
3 changes: 2 additions & 1 deletion xxubin04/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
| 24차시 | 2024.01.29 | DP | <a href="https://www.acmicpc.net/problem/1495">[1495]기타리스트</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/86 |
| 25차시 | 2024.02.01 | 구현 | <a href="https://leetcode.com/problems/roman-to-integer/">[13]Roman to Integer</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/91 |
| 26차시 | 2024.02.05 | 에라토스테네스의 체 | <a href="https://www.acmicpc.net/problem/4948">[4948]베르트랑 공준</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/94 |
| 27차시 | 2024.02.08 | 수학 | <a href="https://www.acmicpc.net/problem/2885">[2885]초콜릿 식사</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/98 |
| 27차시 | 2024.02.08 | 수학 | <a href="https://www.acmicpc.net/problem/2885">[2885]초콜릿 식사</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/98 |
| 28차시 | 2024.02.15 | 이진탐색 | <a href="https://www.acmicpc.net/problem/2805">[2805]나무 자르기</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/108 |