From c0125489e63bb855472055b1d4aaaf67c713ffd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=88=98=ED=98=84?= Date: Thu, 25 Jul 2024 23:05:51 +0900 Subject: [PATCH 1/3] =?UTF-8?q?2024-07-24=20=EB=82=98=EB=AC=B4=20=EC=9E=90?= =?UTF-8?q?=EB=A5=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- suhyun113/README.md | 3 +- .../12-suhyun113.cpp" | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 "suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" diff --git a/suhyun113/README.md b/suhyun113/README.md index 59a2688..ffc876a 100644 --- a/suhyun113/README.md +++ b/suhyun113/README.md @@ -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) | \ No newline at end of file +| 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) | \ No newline at end of file diff --git "a/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" "b/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" new file mode 100644 index 0000000..764f1bd --- /dev/null +++ "b/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" @@ -0,0 +1,51 @@ +// 2805 : 나무 자르기 + +#include +#include +using namespace std; + +// 절단기에 지정한 나무의 높이 H로 나무를 자름 +// H보다 크면 자르기(작으면, 값이 0 또는 음수로 나옴) +bool canCut(vector& 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& 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 trees(N); + for (int i = 0; i < N; i++) { + cin >> trees[i]; + } + + int result = binary_search(trees, M); + cout << result << endl; + + return 0; +} \ No newline at end of file From 8ad4edac049ef031fc3a2f4da9c7394c2559a769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=88=98=ED=98=84?= Date: Fri, 26 Jul 2024 01:06:37 +0900 Subject: [PATCH 2/3] =?UTF-8?q?2024-07-24=20=EB=82=98=EB=AC=B4=20=EC=9E=90?= =?UTF-8?q?=EB=A5=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 함수명 수정 --- .../12-suhyun113.cpp" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" "b/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" index 764f1bd..08a08f5 100644 --- "a/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" +++ "b/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" @@ -6,7 +6,7 @@ using namespace std; // 절단기에 지정한 나무의 높이 H로 나무를 자름 // H보다 크면 자르기(작으면, 값이 0 또는 음수로 나옴) -bool canCut(vector& trees, int H, int M) { +bool can_cut(vector& trees, int H, int M) { long long total = 0; // 자른 나무들의 총 합 for (int tree : trees) { if (tree > H) { @@ -23,7 +23,7 @@ int binary_search(vector& trees, int M) { while (lo + 1 < hi) { int mid = (lo + hi) / 2; // 중앙값 저장 - if (canCut(trees, mid, M)) { // 절단기 높이 변경 + if (can_cut(trees, mid, M)) { // 절단기 높이 변경 lo = mid; // 현재의 절단기 높이를 가능한 최소 높이로 저장(중앙값 더 커짐) } else { From 74486caba5c1e60a865c36267e94d4d659891ed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=88=98=ED=98=84?= Date: Fri, 26 Jul 2024 01:10:55 +0900 Subject: [PATCH 3/3] =?UTF-8?q?2024-07-24=20=EB=82=98=EB=AC=B4=20=EC=9E=90?= =?UTF-8?q?=EB=A5=B4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 공백 제거 --- .../12-suhyun113.cpp" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" "b/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" index 08a08f5..848d4ec 100644 --- "a/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" +++ "b/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" @@ -6,7 +6,7 @@ using namespace std; // 절단기에 지정한 나무의 높이 H로 나무를 자름 // H보다 크면 자르기(작으면, 값이 0 또는 음수로 나옴) -bool can_cut(vector& trees, int H, int M) { +bool can_cut(vector& trees, int H, int M) { long long total = 0; // 자른 나무들의 총 합 for (int tree : trees) { if (tree > H) {