-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ2805.java
More file actions
46 lines (41 loc) ยท 1.6 KB
/
Copy pathBOJ2805.java
File metadata and controls
46 lines (41 loc) ยท 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main.week5.BOJ2805;
import java.io.IOException;
import java.util.Scanner;
/**
* ๋๋ฌด ์๋ฅด๊ธฐ
* https://www.acmicpc.net/problem/2805
* ์ด์งํ์
*
* @author hazel
*/
public class BOJ2805 {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); //๋๋ฌด์ ์. ์ต๋๊ฐ์ด 1,000,000
int m = scanner.nextInt(); //์ง์ผ๋ก ๊ฐ์ ธ๊ฐ๋ ค๋ ๋๋ฌด ๊ธธ์ด. ์ต๋๊ฐ์ด 2,000,000,000
int[] arr = new int[n];
//์ต๋๊ฐ ๊ตฌํ๊ธฐ - ์๋ฌด๋ฆฌ ๊ธธ๊ฒ ์ ๋จํด๋ ์ ์ผ ๊ธด ๋๋ฌด ๊ธธ์ด๋งํผ ์ ๋จ ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ
int maxValue = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt(); //์ต๋๊ฐ 1,000,000,000
maxValue = Math.max(arr[i], maxValue);
}
int left = 0;
int right = maxValue; // ์์ 1์์ ๋ฒ์๋ฅผ๋ 0 ~20
while (left <= right) {
int mid = (left + right) / 2;
long treeLength = 0; //๋ง์ฝ ๋๋ฌด์ ๋์ด๊ฐ 1,000,000,000์ผ๋ ๋์ ๊ฐ์ด int์ ๋ฒ์๋ฅผ ๋์ ์ ์์ผ๋ฏ๋ก long
for (int i = 0; i < n; i++) {
if (arr[i] > mid) { //๋๋ฌด๊ฐ mid์ ๊ธธ์ด๋ณด๋ค ๊ธธ๋๋ง
treeLength += arr[i] - mid;
}
}
if (treeLength >= m) { //๋๋ฌด ๊ธธ์ด์ ํฉ์ ๋ ๋๋ ค์ผํจ
left = mid + 1;
} else { //๋๋ฌด ๊ธธ์ด์ ํฉ์ ์ค์ฌ์ํจ.
right = mid - 1;
}
}
System.out.println(right);
}
}