Skip to content

Commit

Permalink
2024-07-24 나무 자르기
Browse files Browse the repository at this point in the history
  • Loading branch information
suhyun113 committed Jul 25, 2024
1 parent b9621c6 commit c012548
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
3 changes: 2 additions & 1 deletion suhyun113/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
| 8차시 | 2024.05.14 | 수학 | [어린 왕자](https://www.acmicpc.net/problem/1004) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/32) |
| 9차시 | 2024.05.28 | 다익스트라 | [최소비용 구하기](https://www.acmicpc.net/problem/1916) | [#33](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/33) |
| 10차시 | 2024.07.13 | 플로이드-워셜 | [플로이드](https://www.acmicpc.net/problem/11404) | [#36](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/36) |
| 11차시 | 2024.07.16 | BFS | [연구소](https://www.acmicpc.net/problem/14502) | [#39](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/39) |
| 11차시 | 2024.07.16 | BFS | [연구소](https://www.acmicpc.net/problem/14502) | [#39](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/39) |
| 12차시 | 2024.07.24 | 이분 탐색 | [나무 자르기](https://www.acmicpc.net/problem/2805) | [#42](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/42) |
51 changes: 51 additions & 0 deletions suhyun113/이분 탐색/12-suhyun113.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 2805 : 나무 자르기

#include <iostream>
#include <vector>
using namespace std;

// 절단기에 지정한 나무의 높이 H로 나무를 자름
// H보다 크면 자르기(작으면, 값이 0 또는 음수로 나옴)
bool canCut(vector<int>& trees, int H, int M) {
long long total = 0; // 자른 나무들의 총 합
for (int tree : trees) {
if (tree > H) {
total += (tree - H);
}
}
return total >= M; // 총 합이 M 보다는 커야 함
}

int binary_search(vector<int>& trees, int M) {
int lo = 0;
int hi = 1000000000;

while (lo + 1 < hi) {
int mid = (lo + hi) / 2; // 중앙값 저장

if (canCut(trees, mid, M)) { // 절단기 높이 변경
lo = mid; // 현재의 절단기 높이를 가능한 최소 높이로 저장(중앙값 더 커짐)
}
else {
hi = mid; // 현재 절단기 높이 안되면, 더 낮은 높이로 확인
}
}

return lo; // canCut을 만족해야 하므로
}

int main() {
int N, M;
cin >> N >> M;

// N개의 나무들의 길이 목록 만들기
vector<int> trees(N);
for (int i = 0; i < N; i++) {
cin >> trees[i];
}

int result = binary_search(trees, M);
cout << result << endl;

return 0;
}

0 comments on commit c012548

Please sign in to comment.