- 생각
이분 탐색을 이용해서 풀면 간단하게 풀릴 것 같은 문제이다.
- 코드
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Main {
static int[] array;
static int N, M, L, answer;
public static void main(String[] args) throws Exception {
SetData();
BinarySearch(0, L - 1);
System.out.println(answer);
}
// 데이터
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
N = in.readInt();
M = in.readInt();
L = in.readInt();
array = new int[N + 2];
array[N + 1] = L;
for (int i = 1; i <= N; i++)
array[i] = in.readInt();
Arrays.sort(array);
}
private static void BinarySearch(int left, int right) {
while (left <= right) {
int mid = (left + right) / 2;
if (isPossible(mid))
left = mid + 1;
else
right = mid - 1;
}
answer = left;
}
public static boolean isPossible(int mid) {
int sum = 0;
for (int i = 0; i < N + 1; i++) {
sum += (array[i + 1] - array[i] - 1) / mid;
}
return M < sum;
}
private static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 7576번 : 토마토 (0) | 2021.01.26 |
---|---|
[BOJ/JAVA] 백준 1405번 : 미친 로봇 (0) | 2021.01.26 |
[BOJ/JAVA] 백준 14502번 : 연구소 (공부 더 해보기) (0) | 2021.01.24 |
[BOJ/JAVA] 백준 7579번 : 앱 (0) | 2021.01.24 |
[BOJ/JAVA] 백준 15686번 : 치킨 배달 (좀 더 공부해보자) (0) | 2021.01.24 |