Skip to content

Commit e861f25

Browse files
authored
[20250717] BOJ / G4 / 공유기 설치 / 이인희
1 parent 8650ac3 commit e861f25

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.Arrays;
6+
7+
public class B2110 {
8+
private static BufferedReader br;
9+
private static int n;
10+
private static int c;
11+
private static int[] houses;
12+
13+
public static void main(String[] args) throws IOException {
14+
br = new BufferedReader(new InputStreamReader(System.in));
15+
String[] temp = br.readLine().split(" ");
16+
n = Integer.parseInt(temp[0]);
17+
c = Integer.parseInt(temp[1]);
18+
19+
houses = new int[n];
20+
for (int i = 0; i < n; i++) {
21+
houses[i] = Integer.parseInt(br.readLine());
22+
}
23+
24+
Arrays.sort(houses);
25+
26+
int left = 1;
27+
int right = houses[n - 1] - houses[0];
28+
int answer = 0;
29+
30+
while (left <= right) {
31+
int mid = (left + right) / 2;
32+
if (canInstall(mid)) {
33+
answer = mid;
34+
left = mid + 1;
35+
} else {
36+
right = mid - 1;
37+
}
38+
}
39+
40+
System.out.println(answer);
41+
}
42+
43+
private static boolean canInstall(int minDist) {
44+
int count = 1;
45+
int lastInstalled = houses[0];
46+
for (int i = 1; i < n; i++) {
47+
if (houses[i] - lastInstalled >= minDist) {
48+
count++;
49+
lastInstalled = houses[i];
50+
}
51+
}
52+
return count >= c;
53+
}
54+
}
55+
```

0 commit comments

Comments
 (0)