Skip to content

Commit aa09391

Browse files
committed
[Silver II] Title: 나무 자르기, Time: 644 ms, Memory: 133436 KB -BaekjoonHub
1 parent 70ae533 commit aa09391

2 files changed

Lines changed: 27 additions & 32 deletions

File tree

백준/Silver/2805. 나무 자르기/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 144340 KB, 시간: 2136 ms
7+
메모리: 133436 KB, 시간: 644 ms
88

99
### 분류
1010

1111
이분 탐색, 매개 변수 탐색
1212

1313
### 제출 일자
1414

15-
2024년 4월 11일 14:32:49
15+
2026년 2월 25일 21:17:12
1616

1717
### 문제 설명
1818

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
1-
/* /dev/stdin */
2-
let fs = require('fs');
3-
let input = fs.readFileSync('/dev/stdin').toString().split('\n');
1+
// /dev/stdin
2+
const fs = require("fs");
3+
let input = fs
4+
.readFileSync("/dev/stdin")
5+
.toString()
6+
.split("\n")
7+
.map((x) => x.replace("\r", ""));
48

9+
const [N, M] = input[0].split(" ").map(Number);
10+
const trees = input[1].split(" ").map(Number);
11+
let answer = 0;
512

6-
const N = Number(input[0].split(' ')[0]);
7-
const M = Number(input[0].split(' ')[1]);
8-
const tree = input[1].split(' ').map(Number);
9-
10-
let start = 1;
11-
let end = Math.max(...tree);
12-
let result = 0;
13+
let left = 0;
14+
let right = Math.max(...trees);
1315

14-
while(start <= end) {
15-
let mid = parseInt((start + end) / 2);
16-
let total = 0;
17-
for(x of tree) {
18-
let temp = 0;
19-
if (x - mid < 0) {
20-
temp = 0;
21-
} else {
22-
temp = x - mid;
16+
while (left <= right) {
17+
const mid = Math.floor((left + right) / 2);
18+
19+
let high = 0;
20+
for (const tree of trees) {
21+
if (tree >= mid) {
22+
high += tree - mid;
2323
}
24-
25-
total += temp;
2624
}
2725

28-
if(total >= M) {
29-
result = mid;
30-
start = mid + 1;
31-
}
32-
if(total < M) {
33-
end = mid - 1;
26+
if (M <= high) {
27+
answer = mid;
28+
left = mid + 1;
29+
} else {
30+
right = mid - 1;
3431
}
35-
36-
3732
}
3833

39-
console.log(result);
34+
console.log(answer);

0 commit comments

Comments
 (0)