ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

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

 

[๋ฌธ์ œ]

[ํ’€์ด]

0๋ฒˆ์งธ์ง‘ ~ N-1๋ฒˆ์งธ ์ง‘๊นŒ์ง€ ์ด๋ถ„ํƒ์ƒ‰์„ ํ†ตํ•ด interval ๊ฐ’์„ ์ด์šฉํ•ด ๊ณต์œ ๊ธฐ๋ฅผ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ฉฐ interval๊ฐ’์„ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์ง‘์˜ ๋ฒˆํ˜ธ๋“ค์ด 1 2 8 4 9 ์ด๊ณ  ๊ณต์œ ๊ธฐ๋Š” 3๊ฐœ๋ฉด, ์ตœ๋Œ€๊ฐ’์€ (9-1) / 2 = 4์ž…๋‹ˆ๋‹ค. ๋งŒ์•ฝ 1~9๊ฐ€ ๋ชจ๋‘ ์ฑ„์›Œ์ ธ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด

1 5 9๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

  • home[] ์— N๊ฐœ์˜ ์ง‘ ์œ„์น˜๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  •  
  • ๊ณต์œ ๊ธฐ ์„ค์น˜ ๊ฐ„๊ฒฉ interval์˜ ์ตœ์†Œ๊ฐ’๊ณผ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜์—ฌ left, right์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    • ์ตœ์†Œ๊ฐ’ 1์„ left์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    • ์ตœ๋Œ€๊ฐ’์€ (home[N-1] - home[0]) / (C-1) ์œผ๋กœ ๊ตฌํ•˜์—ฌ right์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  • left์™€ right์˜ ์ค‘๊ฐ„๊ฐ’ mid๋ฅผ interval๋กœ ์„ค์ •ํ•˜์—ฌ wifi(mid)๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ๊ฐ’์˜ ์„ค์น˜ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ, ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด left++ํ•˜๊ณ  ์—†๋‹ค๋ฉด right--๋ฅผ ํ†ตํ•ด mid๊ฐ’์„ ๊ฐฑ์‹ ํ•ด๊ฐ€๋ฉฐ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

[์ฝ”๋“œ]

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

public class Main {
    static int N;
    static int C;
    static int home[];
    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());
        home = new int[N];
        for(int i=0;i<N;i++) {
            home[i] = Integer.parseInt(br.readLine());
        }
        Arrays.sort(home);
        int start = home[0];
        int end = home[N-1];
        int left = 1;
        int right = (end - start)/(C-1);
        while(left <= right) {
            int mid = (left + right)/2;
            boolean find = wifi(mid);
            if(find) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        System.out.println(right);
    }
    public static boolean wifi(int interval) {
        int cnt = 1;
        int index = 0;
        int now = home[0] + interval;
        while(cnt < C) {
            if(now > home[N-1]) {
                break;
            }
            for(int i=index + 1;i < N;i++) {
                if(home[i] >= now) {
                    cnt++;
                    now = home[i] + interval;
                    index = i;
                }
                if(cnt == C) {
                    return true;
                }
            }
        }
        if(cnt == C) {
            return true;
        }
        return false;
    }
}