๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฐฑ์ค€

[๋ฐฑ์ค€ ์Šคํ„ฐ๋”” 1์ฃผ์ฐจ] 2110๋ฒˆ ๊ณต์œ ๊ธฐ์„ค์น˜ ๊ณจ๋“œ5 - ์ž๋ฐ”(JAVA) / ์ด๋ถ„ํƒ์ƒ‰

๐Ÿ“‘ ๋ฌธ์ œ

https://www.acmicpc.net/problem/2110

 

2110๋ฒˆ: ๊ณต์œ ๊ธฐ ์„ค์น˜

์ฒซ์งธ ์ค„์— ์ง‘์˜ ๊ฐœ์ˆ˜ N (2 ≤ N ≤ 200,000)๊ณผ ๊ณต์œ ๊ธฐ์˜ ๊ฐœ์ˆ˜ C (2 ≤ C ≤ N)์ด ํ•˜๋‚˜ ์ด์ƒ์˜ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์ง‘์˜ ์ขŒํ‘œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” xi (0 ≤ xi ≤ 1,000,000,000)๊ฐ€

www.acmicpc.net

๋”๋ณด๊ธฐ

๋ฌธ์ œ

๋„ํ˜„์ด์˜ ์ง‘ N๊ฐœ๊ฐ€ ์ˆ˜์ง์„  ์œ„์— ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ง‘์˜ ์ขŒํ‘œ๋Š” x1, ..., xN์ด๊ณ , ์ง‘ ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ๊ฐ™์€ ์ขŒํ‘œ๋ฅผ ๊ฐ€์ง€๋Š” ์ผ์€ ์—†๋‹ค.

๋„ํ˜„์ด๋Š” ์–ธ์ œ ์–ด๋””์„œ๋‚˜ ์™€์ดํŒŒ์ด๋ฅผ ์ฆ๊ธฐ๊ธฐ ์œ„ํ•ด์„œ ์ง‘์— ๊ณต์œ ๊ธฐ C๊ฐœ๋ฅผ ์„ค์น˜ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ตœ๋Œ€ํ•œ ๋งŽ์€ ๊ณณ์—์„œ ์™€์ดํŒŒ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ ์ง‘์—๋Š” ๊ณต์œ ๊ธฐ๋ฅผ ํ•˜๋‚˜๋งŒ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๊ณ , ๊ฐ€์žฅ ์ธ์ ‘ํ•œ ๋‘ ๊ณต์œ ๊ธฐ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐ€๋Šฅํ•œ ํฌ๊ฒŒ ํ•˜์—ฌ ์„ค์น˜ํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

C๊ฐœ์˜ ๊ณต์œ ๊ธฐ๋ฅผ N๊ฐœ์˜ ์ง‘์— ์ ๋‹นํžˆ ์„ค์น˜ํ•ด์„œ, ๊ฐ€์žฅ ์ธ์ ‘ํ•œ ๋‘ ๊ณต์œ ๊ธฐ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ตœ๋Œ€๋กœ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ง‘์˜ ๊ฐœ์ˆ˜ N (2 ≤ N ≤ 200,000)๊ณผ ๊ณต์œ ๊ธฐ์˜ ๊ฐœ์ˆ˜ C (2 ≤ C ≤ N)์ด ํ•˜๋‚˜ ์ด์ƒ์˜ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์ง‘์˜ ์ขŒํ‘œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” xi (0 ≤ xi ≤ 1,000,000,000)๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๊ฐ€์žฅ ์ธ์ ‘ํ•œ ๋‘ ๊ณต์œ ๊ธฐ ์‚ฌ์ด์˜ ์ตœ๋Œ€ ๊ฑฐ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

 

 

 

 

 

โœ’๏ธ ํ’€์ด

 ๐Ÿ’ก ์ตœ๋Œ€ํ•œ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฒŒ๋ ค์„œ ๊ณต์œ ๊ธฐ๋ฅผ ์„ค์น˜ํ•˜๋Š”๊ฒŒ ๋ชฉ์ ์ด๋‹ค.

 ๋”ฐ๋ผ์„œ ๊ณต์œ ๊ธฐ๋ฅผ ์„ค์น˜ํ•  ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Œ๊ฐ’(mid)์„ ์ •ํ•ด๋†“๊ณ , ๊ทธ ์ด์ƒ์ด ๋  ๋•Œ๋งŒ ๊ณต์œ ๊ธฐ๋ฅผ ์„ค์น˜ํ•œ๋‹ค. ๊ทธ ์ดํ•˜์˜ ๊ฑฐ๋ฆฌ์—์„œ ์„ค์น˜ํ•˜๋ฉด, ์ตœ๋Œ€ํ•œ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฒŒ๋ฆฌ๊ฒ ๋‹ค๋Š” ๋ชฉ์ ์„ ์ƒ์‹คํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 ์ด๋ฒˆ ์ด๋ถ„ํƒ์ƒ‰์—์„œ ์กฐ์ ˆํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ ์ด ์ตœ์†Œ๊ฐ’์ด๋‹ค.

 

houses = new int[N];
for (int i = 0; i < N; i++) {
	houses[i] = Integer.parseInt(br.readLine());
}//input
Arrays.sort(houses);

 N๊ฐœ์˜ ์ง‘์ด ์œ„์น˜ํ•œ ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•ด๋‘๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.

 ๋”ฐ๋ผ์„œ house[0]์ด ์ œ์ผ ์™ผ์ชฝ์— ์œ„์น˜ํ•œ ์ง‘, house[N-1]์ด ์ œ์ผ ์˜ค๋ฅธ์ชฝ์— ์œ„์น˜ํ•œ ์ง‘์ด ๋  ๊ฒƒ์ด๋‹ค.

 

 ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ์ด์ œ ์ด๋ถ„ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค.

 ์ฒ˜์Œ mid๊ฐ’์€ ์ตœ๋Œ€๊ธฐ์ค€๊ฑฐ๋ฆฌ๋กœ, ๋งˆ์„ ์ „์ฒด ๊ธธ์ด(์ฒซ ๋ฒˆ์งธ ์ง‘๊ณผ ๋งˆ์ง€๋ง‰ ์ง‘ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ)/2๋กœ ์„ค์ •ํ•œ๋‹ค. 

 

 

 

 

 

โœ”๏ธ ์˜ˆ๋ฅผ ๋“ค์–ด N=4์ด๊ณ , C=3, ๊ฐ ์ง‘์˜ ์œ„์น˜๊ฐ€ [10,15,40,50]์ด๋ผ๊ณ  ํ•  ๋•Œ๋ฅผ ์ƒ๊ฐํ•ด๋ณธ๋‹ค.

 ์ฒ˜์Œ mid๊ฐ’์€ ์ฒ˜์Œ ์ง‘๊ณผ ๋งˆ์ง€๋ง‰ ์ง‘ ์‚ฌ์ด์˜ ์ค‘๊ฐ„๊ฑฐ๋ฆฌ์ธ (10+50)/2=30์ผ ๊ฒƒ์ด๋‹ค.

 โ—ป๏ธ ์ฒ˜์Œ ๊ณต์œ ๊ธฐ ์„ค์น˜ ์œ„์น˜๋Š” ์ฒซ ๋ฒˆ์งธ ์ง‘์˜ ์œ„์น˜์ธ 10์ด ๋œ๋‹ค.

 โ—ป๏ธ ๋‘ ๋ฒˆ์งธ ์ง‘์ธ 15์˜ ๊ฒฝ์šฐ, ์ด์ „ ์ง‘ 10๊ณผ์˜ ๊ฑฐ๋ฆฌ์ฐจ๊ฐ€ 5๋ฐ–์— ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ธฐ์ค€๊ธธ์ด์ธ 30์„ ๋„˜์ง€ ๋ชปํ•˜์—ฌ ๊ณต์œ ๊ธฐ๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 โ—ป๏ธ ์„ธ ๋ฒˆ์งธ ์ง‘์ธ 40์˜ ๊ฒฝ์šฐ ์ด์ „ ์ง‘ 10๊ณผ์˜ ๊ฑฐ๋ฆฌ์ฐจ๊ฐ€ 30์ด๋ฏ€๋กœ ๊ธฐ์ค€์„ ๋งŒ์กฑํ•˜์—ฌ ๊ณต์œ ๊ธฐ๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ๊ธฐ์ค€์œ„์น˜๋Š” ์ƒˆ๋กœ ๊ณต์œ ๊ธฐ๋ฅผ ์„ค์น˜ํ•œ ์œ„์น˜์ธ 40์œผ๋กœ ์˜ฎ๊ฒจ๊ฐ„๋‹ค.

 โ—ป๏ธ ๋งˆ์ง€๋ง‰ ์ง‘์ธ 50์˜ ๊ฒฝ์šฐ ์ด์ „ ์ง‘ 40๊ณผ์˜ ๊ฑฐ๋ฆฌ์ฐจ์ด๊ฐ€ 10๋ฐ–์— ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ณต์œ ๊ธฐ๋ฅผ ์„ค์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

 ๊ณต์œ ๊ธฐ๋ฅผ ๋‘ ๊ฐœ ์„ค์น˜ํ–ˆ์œผ๋ฏ€๋กœ ๋ชฉํ‘œ๊ฐ’ 3๋ณด๋‹ค ๋ชจ์ž๋ผ๋‹ค. ์ฆ‰, ๊ฑฐ๋ฆฌ๋ฅผ ๋„ˆ๋ฌด ๋ฒŒ๋ ค์„œ ์„ค์น˜ํ–ˆ๋‹ค๋Š” ๋œป์ด๋‹ค.

 ์ด๋Ÿด ๋•Œ์—๋Š” ๊ธฐ์ค€๊ธธ์ด๋ฅผ ์ขํ˜€์„œ ๋‹ค์‹œ ํƒ์ƒ‰์„ ์‹œ๋„ํ•ด๋ณธ๋‹ค.

 

if(cnt>=C) {//์ง‘ ์‚ฌ์ด ๊ฑฐ๋ฆฌ๋ฅผ ์ ๋‹นํžˆ ์„ค์ •ํ–ˆ์„ ๋•Œ
	result = mid;
				
	//์ตœ์ ์˜ ํ•ด๋ฅผ ์œ„ํ•ด ๊ฐ„๊ฒฉ์„ ๋” ๋„“ํ˜€๋ณธ๋‹ค
	left = mid+1;
}else {//์ง‘ ์‚ฌ์ด ๊ฑฐ๋ฆฌ๋ฅผ ๋„ˆ๋ฌด ๋„“๊ฒŒ ์„ค์ •ํ–ˆ์„ ๋•Œ
	//๊ฐ„๊ฒฉ์„ ๋” ์ขํ˜€๋ณธ๋‹ค.
	right = mid-1;
}

 

 

 

 

 

 

๐Ÿ™‹‍โ™€๏ธ ์˜๊ฒฌ

 ์ˆ˜ํ•™์„ ์ „ํ˜€ ๋ชปํ•˜๋Š” ๋‚˜๋กœ์„œ๋Š” mid๊ฐ’์„ ์กฐ์œจํ•  ๋•Œ ์™œ left=(mid+1)/ right=(mid-1)๊ฐ™์ด ํ‘ธ๋Š” ๊ฒƒ์ธ์ง€ ์ดํ•ดํ•˜์ง€ ๋ชปํ–ˆ๋‹ค...

 ๊ฑฐ๋ฆฌ๋ฅผ ์ขํžˆ๋ ค๋ฉด left+1์ด๋‚˜ right-1๋กœ ํ•ด๋„ ๋˜์ž–์•„? 

 ํ•˜์ง€๋งŒ ์ง์ ‘ ์ฐ์–ด๋ณด๋‹ˆ ์„ฑ๋Šฅ์— ์žˆ์–ด์„œ ์ •๋ง ํฐ ์ฐจ์ด๊ฐ€ ์ƒ๊ธด๋‹ค.

 ์ด๋ถ„ํƒ์ƒ‰์˜ ์˜์˜๋ฅผ ์ „ํ˜€ ์ƒ๊ฐํ•ด๋ณด์ง€ ๋ชปํ–ˆ๋‹ค๋Š” ์ฆ๋ช…....(^^)

 ์ฐ”๋”์ฐ”๋” ์กฐ์œจํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์ด๋ถ„ํƒ์ƒ‰์œผ๋กœ ์ฃผ์–ด์ง„ ๊ฐ’์—์„œ ํฌ๊ฒŒํฌ๊ฒŒ ์˜ฎ๊ฒจ๊ฐ€๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ์„ฑ๋Šฅ์— ์ข‹๋‹ค.

 ๊ทธ๋ƒฅ ์™ธ์šฐ์ž!!!

 

 

 

 

 

 

 

 

๐Ÿ’ป ์†Œ์Šค์ฝ”๋“œ: ์ž๋ฐ”(Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

// ์ด๋ถ„ํƒ์ƒ‰, ๋งค๊ฐœ ๋ณ€์ˆ˜ ํƒ์ƒ‰
public class Main_๋ฐฑ์ค€_2110_๊ณต์œ ๊ธฐ์„ค์น˜_๊ณจ๋“œV {

	static int N, C;
	static int[] houses;
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		N = Integer.parseInt(st.nextToken());
		C = Integer.parseInt(st.nextToken());
		houses = new int[N];
		for (int i = 0; i < N; i++) {
			houses[i] = Integer.parseInt(br.readLine());
		}//input
		Arrays.sort(houses);

		
		//1.์™„์ „ํƒ์ƒ‰
		/*int max = houses[N-1]-houses[0]; //์ตœ๋Œ€๊ฐ„๊ฒฉ
		int dist = 1; //์‹œ์ž‘๊ฐ„๊ฒฉ
		
		while(dist<=max) {
			// ์ฒซ ๋ฒˆ์งธ ๊ณต์œ ๊ธฐ
			int cnt = 1;
			int before_pos = houses[0];
			
			// ๊ณต์œ ๊ธฐ ์„ค์น˜์ž‘์—…
			for (int i = 1; i < N; i++) {
				// ๊ฐ„๊ฒฉ์ด ๊ณต์œ ๊ธฐ๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋„๋ผ๋ฉด
				if(houses[i]-before_pos >= dist) {
					cnt++;
					before_pos = houses[i];
				}
			}
			
			if(cnt<C) break;
			dist++;
		}
		System.out.println(dist-1);*/
		
		//2.์ด๋ถ„ํƒ์ƒ‰
		int result = 0;
		int left = 0;
		int right = houses[N-1]-houses[0]; //์ตœ๋Œ€๊ฐ„๊ฒฉ
		
		while(left<=right) {
			// ์ฒซ ๋ฒˆ์งธ ๊ณต์œ ๊ธฐ
			int cnt = 1;
			int before_pos = houses[0];
			
			// ์ง‘ ์‚ฌ์ด ๊ฑฐ๋ฆฌ -> ์ด๋ถ„ํƒ์ƒ‰์—์„œ ์กฐ์ ˆํ•  ๋ณ€์ˆ˜
			int mid = (right+left)/2;
			
			// ๊ณต์œ ๊ธฐ ์„ค์น˜์ž‘์—…
			for (int i = 1; i < N; i++) {
				if(houses[i]-before_pos >= mid) {
					cnt++;
					before_pos = houses[i];
				}
			}
			
			if(cnt>=C) {//์ง‘ ์‚ฌ์ด ๊ฑฐ๋ฆฌ๋ฅผ ์ ๋‹นํžˆ ์„ค์ •ํ–ˆ์„ ๋•Œ
				result = mid;
				
				//์ตœ์ ์˜ ํ•ด๋ฅผ ์œ„ํ•ด ๊ฐ„๊ฒฉ์„ ๋” ๋„“ํ˜€๋ณธ๋‹ค
				left = mid+1;
			}else {//์ง‘ ์‚ฌ์ด ๊ฑฐ๋ฆฌ๋ฅผ ๋„ˆ๋ฌด ๋„“๊ฒŒ ์„ค์ •ํ–ˆ์„ ๋•Œ
				//๊ฐ„๊ฒฉ์„ ๋” ์ขํ˜€๋ณธ๋‹ค.
				right = mid-1;
			}
		}
		
		System.out.println(result);
	}//end of main
}//end of class

 

 

 

 

 

 

 

 

 

๐Ÿ’Ž ๐Ÿ’Ž ๐Ÿ’Ž

 

 

๏ผฐ๏ฝ๏ฝ“๏ฝ”๏ฝ…๏ฝ„ ๏ผข๏ฝ™ ๏ผณ๏ผก๏ผน

๐˜›๐˜ฉ๐˜ข๐˜ฏ๐˜ฌ๐˜ด ๐˜ง๐˜ฐ๐˜ณ ๐˜ณ๐˜ฆ๐˜ข๐˜ฅ๐˜ช๐˜ฏ๐˜จ