Skip to content

Commit 43e8267

Browse files
authored
Merge pull request #518 from AlgorithmWithGod/Seol-JY
[20250721] BOJ / G4 / 공유기 설치 / 설진영
2 parents 3319ddb + f4e38f9 commit 43e8267

File tree

1 file changed

+53
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)