Skip to content

Commit

Permalink
Merge pull request #42 from AlgoLeadMe/12-suhyun113
Browse files Browse the repository at this point in the history
12-suhyun113
  • Loading branch information
suhyun113 authored Sep 9, 2024
2 parents 3028d54 + 74486ca commit 72fc116
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 can_cut(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 (can_cut(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 72fc116

Please sign in to comment.