2110번: 공유기 설치

첫째 줄에 집의 개수 N (2 ≤ N ≤ 200,000)과 공유기의 개수 C (2 ≤ C ≤ N)이 하나 이상의 빈 칸을 사이에 두고 주어진다. 둘째 줄부터 N개의 줄에는 집의 좌표를 나타내는 xi (0 ≤ xi ≤ 1,000,000,000)가

www.acmicpc.net

 

 


 

 

  • 생각

1. 단순 O(n^2)으로는 시간 초과가 뜰 것 같다.

 

2. 이분 탐색을 사용해도 될 것 같다. 특정한 간격을 기준으로 가능한 위치에 공유기 설치, 설치 후 공유기 수가 더 설치되야하면 간격을 줄임. 공유기 수를 줄여야 한다면 간격을 늘림.

 

 

 

  • 코드

정답 코드 : 이분탐색을 통해 구현

 

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main {
	static int N, C;
	static int[] array;

	public static void main(String[] args) throws Exception {
		SetData();
		System.out.println(SearchBinary());
	}

	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);

		N = in.nextInt();
		C = in.nextInt();

		array = new int[N];
		for (int i = 0; i < N; i++)
			array[i] = in.nextInt();
		Arrays.sort(array);
	}

	private static int SearchBinary() {
		int left = 1;
		int right = array[N - 1] - array[0];
		int d = 0;
		int value = 0;

		while (left <= right) {
			int mid = (left + right) / 2;
			int start = array[0];
			int count = 1;
			for (int i = 0; i < N; i++) { // 집집마다 검색함.
				d = array[i] - start;
				if (d >= mid) { // 만약 첫번째 집과의 거리가 더 크다면 찾았다고 count 올려주고, 내가 찾는집에 이번 집을 넣어준다.
					count++;
					start = array[i];
				}
			}

			if (count >= C) {
				value = mid;
				left = mid + 1;
			} else {
				right = mid - 1;
			}
		}
		return value;
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

+ Recent posts