-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Silver II] Title: 나무 자르기, Time: 2284 ms, Memory: 145236 KB -BaekjoonHub
- Loading branch information
1 parent
d63797c
commit e7afba1
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# [Silver II] 나무 자르기 - 2805 | ||
|
||
[문제 링크](https://www.acmicpc.net/problem/2805) | ||
|
||
### 성능 요약 | ||
|
||
메모리: 145236 KB, 시간: 2284 ms | ||
|
||
### 분류 | ||
|
||
이분 탐색, 매개 변수 탐색 | ||
|
||
### 제출 일자 | ||
|
||
2024년 8월 12일 23:01:17 | ||
|
||
### 문제 설명 | ||
|
||
<p>상근이는 나무 M미터가 필요하다. 근처에 나무를 구입할 곳이 모두 망해버렸기 때문에, 정부에 벌목 허가를 요청했다. 정부는 상근이네 집 근처의 나무 한 줄에 대한 벌목 허가를 내주었고, 상근이는 새로 구입한 목재절단기를 이용해서 나무를 구할것이다.</p> | ||
|
||
<p>목재절단기는 다음과 같이 동작한다. 먼저, 상근이는 절단기에 높이 H를 지정해야 한다. 높이를 지정하면 톱날이 땅으로부터 H미터 위로 올라간다. 그 다음, 한 줄에 연속해있는 나무를 모두 절단해버린다. 따라서, 높이가 H보다 큰 나무는 H 위의 부분이 잘릴 것이고, 낮은 나무는 잘리지 않을 것이다. 예를 들어, 한 줄에 연속해있는 나무의 높이가 20, 15, 10, 17이라고 하자. 상근이가 높이를 15로 지정했다면, 나무를 자른 뒤의 높이는 15, 15, 10, 15가 될 것이고, 상근이는 길이가 5인 나무와 2인 나무를 들고 집에 갈 것이다. (총 7미터를 집에 들고 간다) 절단기에 설정할 수 있는 높이는 양의 정수 또는 0이다.</p> | ||
|
||
<p>상근이는 환경에 매우 관심이 많기 때문에, 나무를 필요한 만큼만 집으로 가져가려고 한다. 이때, 적어도 M미터의 나무를 집에 가져가기 위해서 절단기에 설정할 수 있는 높이의 최댓값을 구하는 프로그램을 작성하시오.</p> | ||
|
||
### 입력 | ||
|
||
<p>첫째 줄에 나무의 수 N과 상근이가 집으로 가져가려고 하는 나무의 길이 M이 주어진다. (1 ≤ N ≤ 1,000,000, 1 ≤ M ≤ 2,000,000,000)</p> | ||
|
||
<p>둘째 줄에는 나무의 높이가 주어진다. 나무의 높이의 합은 항상 M보다 크거나 같기 때문에, 상근이는 집에 필요한 나무를 항상 가져갈 수 있다. 높이는 1,000,000,000보다 작거나 같은 양의 정수 또는 0이다.</p> | ||
|
||
### 출력 | ||
|
||
<p>적어도 M미터의 나무를 집에 가져가기 위해서 절단기에 설정할 수 있는 높이의 최댓값을 출력한다.</p> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
[시간 복잡도 분석] | ||
O(NxM) = 10^6 x 2 x 10^10 =10^16 | ||
=> 말이 안됨 1초에 1억 연산 훨씬 뛰어넘음..! | ||
=> 최대한 승수를 낮추기 위해, O(N x log(M)) | ||
=> 나무 높이쪽을 binary search 해야함 | ||
[이럴때 binary search를 하는군] | ||
- 최적의 값을 찾아감 | ||
- 예시) 누적합 ↓, height ↑ 방향으로 학습시키기 | ||
""" | ||
|
||
N, M = map(int, input().split()) | ||
trees = list(map(int, input().split())) | ||
trees.sort(reverse=True) # 내림차순 정렬 | ||
# TODO: 최적화해볼수도? 더 이상 remain 없으면 sum 그만두기 | ||
|
||
max_height = max(trees) | ||
# heights = list(range(max_height, -1, -1)) | ||
|
||
prev_candidate_height = -1 | ||
|
||
|
||
def get_remains(height): | ||
acc = 0 | ||
for tree in trees: | ||
if tree > height: | ||
acc += tree - height | ||
|
||
return acc | ||
|
||
|
||
def binary_search( | ||
start_idx, | ||
end_idx, | ||
): | ||
|
||
global prev_candidate_height | ||
if start_idx > end_idx: | ||
return | ||
|
||
if start_idx == end_idx: | ||
height = max_height - start_idx | ||
if get_remains(height) >= M: # 후보 아니면, 갱신 못함 | ||
if height > prev_candidate_height: | ||
prev_candidate_height = height | ||
return | ||
|
||
mid_idx = (start_idx + end_idx) // 2 | ||
height = max_height - mid_idx | ||
acc = get_remains(height) | ||
|
||
# binary search 시작; (좌/우 이동 기준) 재밌음 | ||
if acc >= M: # acc ↓, height ↑ | ||
# 아직 충분히 많이 잘랐으니, 덜 자르는 쪽으로 | ||
# 다만, 갱신은 요구사항보다 큰 값일때 | ||
if height > prev_candidate_height: | ||
prev_candidate_height = height | ||
binary_search(start_idx, mid_idx) | ||
else: # acc ↑, height ↓ | ||
binary_search(mid_idx + 1, end_idx) | ||
|
||
|
||
binary_search(0, max_height) | ||
print(f"{prev_candidate_height}") |