Skip to content

Commit ee271e6

Browse files
authored
Merge pull request #251 from AlgorithmWithGod/khj20006
[20250317] BOJ / G4 / 공유기 설치 / 권혁준
2 parents c6dd663 + e3778e0 commit ee271e6

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
public class Main {
7+
8+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
static StringTokenizer st = new StringTokenizer("");
11+
12+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
13+
static int nextInt() throws Exception {
14+
if(!st.hasMoreTokens()) nextLine();
15+
return Integer.parseInt(st.nextToken());
16+
}
17+
static long nextLong() throws Exception {
18+
if(!st.hasMoreTokens()) nextLine();
19+
return Long.parseLong(st.nextToken());
20+
}
21+
static void ioClose() throws Exception {bw.flush();bw.close();br.close();}
22+
23+
/*
24+
* [solving strategy]
25+
* 주어진 문제를 살짝 변형하여, 다음과 같이 생각했습니다.
26+
* "가장 인접한 가스 설치 지점 사이의 최대 거리가 m이 되도록 C개의 방역 가스를 설치할 수 있을까?"
27+
* 위 문제는 '결정 문제'로, 단순 O(N)에 가능 여부를 판별할 수 있습니다.
28+
* 또한, 어떤 m에 대해 가능했다면 m이하의 값들에 대해서도 항상 가능함이 보장됩니다.
29+
* -> 유효한 m값의 범위를 매 step마다 반씩 줄일 수 있습니다.
30+
* 이분 탐색으로 m의 최댓값을 찾아주었습니다.
31+
*
32+
* [description]
33+
* - N, C : 집, 방역 가스의 개수
34+
* - X : 집의 위치 정보 저장
35+
*
36+
* - ready() : input 처리 및 집 위치 X를 정렬
37+
* - solve() : 이분 탐색으로 범위를 좁혀가며 해 도출하기
38+
* - isPossible(m) : m이 유효한 값인지 가능 여부를 판별하여 boolean 형태로 반환
39+
*/
40+
41+
static int N, C;
42+
static int[] X;
43+
44+
public static void main(String[] args) throws Exception {
45+
//--------------솔루션 코드를 작성하세요.--------------------------------
46+
47+
ready();
48+
solve();
49+
50+
ioClose();
51+
52+
}
53+
54+
static void ready() throws Exception {
55+
56+
N = nextInt();
57+
C = nextInt();
58+
X = new int[N];
59+
for(int i=0;i<N;i++) X[i] = nextInt();
60+
Arrays.sort(X);
61+
62+
}
63+
64+
static void solve() throws Exception {
65+
66+
int s = 0, e = X[N-1] - X[0], m = (s+e+1)>>1;
67+
while(s<e) {
68+
if(isPossible(m)) s = m;
69+
else e = m-1;
70+
m = (s+e+1)>>1;
71+
}
72+
bw.write(m + "\n");
73+
74+
}
75+
76+
static boolean isPossible(int dist) {
77+
int cnt = 1, prev = X[0];
78+
for(int i=1;i<N;i++) {
79+
if(X[i]-prev >= dist) {
80+
cnt++;
81+
prev = X[i];
82+
}
83+
}
84+
return cnt >= C;
85+
}
86+
87+
}
88+
89+
90+
```

0 commit comments

Comments
 (0)