`Binary search
저번 스터디를 통해 이분 탐색을 알게 되었고 조건식 및 부등호 여부만 잘 생각하면 되는 문제였다.
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
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B1654 {
static int N, M, cnt = 0;
static long[] arr;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
long max = 0;
arr = new long[N];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(br.readLine());
max = Math.max(arr[i], max);
}
binarySearch(1,max);
}
private static void binarySearch(long start, long end) {
long mid = 0;
while (start <= end) {
int cnt = 0;
mid = (start + end) / 2;
// 개수를 찾는 과정
for(int i =0;i<N;i++) {
cnt+=arr[i]/mid;
}
if (cnt < M) {
end = mid - 1;
} else {
start = mid + 1;
}
// 최대 길이를 구하므로 여기서 부등호
}
System.out.println(start-1);
}
}
|