[BOJ] #2110 ๊ณต์œ ๊ธฐ ์„ค์น˜ (์ด๋ถ„ํƒ์ƒ‰, Python)

    ๋ฐ˜์‘ํ˜•

    ๐ŸŒฑ ๋ฌธ์ œ

     

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

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

    www.acmicpc.net

     

    ๐ŸŒฑ ํ’€์ด

    ์ด ๋ฌธ์ œ๋Š” ์ด๋ถ„ํƒ์ƒ‰ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ์—์„œ๋Š” ์ง‘๋“ค ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ์ด๋ถ„ํƒ์ƒ‰ํ•˜์—ฌ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ์ž์„ธํžˆ ์„ค๋ช…ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    0. ์ž…๋ ฅ ๋ฐ›๊ธฐ

    n, c = map(int, input().split())
    position = [int(input()) for _ in range(n)]
    position.sort()

     

    • ์ง‘์˜ ๊ฐœ์ˆ˜ n๊ณผ ๊ณต์œ ๊ธฐ ๊ฐœ์ˆ˜ c๋ฅผ ์ž…๋ ฅ ๋ฐ›์Šต๋‹ˆ๋‹ค.
    • ์ง‘์˜ ์œ„์น˜๋ฅผ ๋ฆฌ์ŠคํŠธ(position)์— ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค.
    • ์ด๋ถ„ ํƒ์ƒ‰์„ ์œ„ํ•ด์„œ ์ง‘์˜ ์œ„์น˜๋ฅผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

     

    1. ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”

    result = 0
    start,end = 1, position[n-1] - position[0]
    • ๋ฌธ์ œ์˜ ์ •๋‹ต์ธ ๊ณต์œ ๊ธฐ์˜ ์ตœ๋Œ€ ๊ฐ„๊ฒฉ์„ ์ €์žฅํ•˜๋Š” result ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
    • ์ด๋ถ„ ํƒ์ƒ‰์„ ์œ„ํ•œ start์™€ end๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, ๊ฐ„๊ฒฉ์„ ์ด๋ถ„ํƒ์ƒ‰ํ•˜์—ฌ ์กฐ์ •ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— start์˜ ์ดˆ๊ธฐ๊ฐ’์€ ๊ฐ„๊ฒฉ์˜ ์ตœ์†Ÿ๊ฐ’์ธ 1์ด๊ณ , end์˜ ์ดˆ๊ธฐ๊ฐ’์€ ๊ฐ„๊ฒฉ์˜ ์ตœ๋Œ“๊ฐ’์ธ position[n - 1] - position[0] ์ž…๋‹ˆ๋‹ค.

     

    2. ์ด๋ถ„ ํƒ์ƒ‰

    while(start < end):
        mid = (start + end)//2
    
        # ์ฒซ๋ฒˆ์งธ ์ง‘์—๋Š” ๋ฌด์กฐ๊ฑด ์„ค์น˜
        count = 1
        installed = position[0]
    
        for i in range(n):
            if position[i] - installed >= mid:
                count += 1
                installed = position[i]    # ๊ณต์œ ๊ธฐ ์„ค์น˜
        if count >= c:
            result = mid
            start = mid + 1     # ๊ฐ„๊ฒฉ ๋Š˜๋ฆฌ๊ธฐ
        elif count < c:
            end = mid       # ๊ฐ„๊ฒฉ ์ค„์ด๊ธฐ
    • ์ค‘๊ฐ„๊ฐ’(mid)๋Š” (start + end) // 2์ž…๋‹ˆ๋‹ค. ์ฆ‰, ๊ฐ„๊ฒฉ์˜ ์ค‘๊ฐ„๊ฐ’์ž…๋‹ˆ๋‹ค. (start๊ฐ€ ๊ฐ„๊ฒฉ์˜ ์ตœ์†Ÿ๊ฐ’, end๊ฐ€ ๊ฐ„๊ฒฉ์˜ ์ตœ๋Œ“๊ฐ’)
    • ๊ณต์œ ๊ธฐ๋ฅผ ์ตœ๋Œ€ํ•œ ๋„์ฐ๋„์ฐํ•˜๊ฒŒ ์„ค์น˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ฒซ๋ฒˆ์งธ ์ง‘์—๋Š” ๋ฌด์กฐ๊ฑด ์„ค์น˜๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (๋‹จ, ๋งˆ์ง€๋ง‰ ์ง‘์—๋Š” ๋ฌด์กฐ๊ฑด ์„ค์น˜ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ while ๋ฃน์œผ๋กœ ๋“ค์–ด๊ฐ€๋ฉด ๊ณต์œ ๊ธฐ ์„ค์น˜ ๊ฐœ์ˆ˜๋ฅผ ์ƒˆ๋Š” count ๋ณ€์ˆ˜๋ฅผ ๋ฌด์กฐ๊ฑด 1๋กœ ์„ค์ •ํ•˜๊ณ  position[0]์„ ๊ณต์œ ๊ธฐ ์„ค์น˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•˜๋Š” installed ๋ณ€์ˆ˜์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค. 
    • ๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ, ์ „์ฒด position์„ ๋Œ๋ฉด์„œ ํ˜„์žฌ ์„ค์ •๋œ mid ๊ฐ’๋ณด๋‹ค ๊ฐ„๊ฒฉ์ด ๋„“๊ฑฐ๋‚˜ ๊ฐ™์€ ์œ„์น˜์— ๊ณต์œ ๊ธฐ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
      • ๊ณต์œ ๊ธฐ๋ฅผ ์„ค์น˜ํ–ˆ์œผ๋ฉด count ๊ฐ’์„ 1 ์ฆ๊ฐ€์‹œํ‚ค๊ณ  installed๋Š” ์„ค์น˜ํ•œ ๊ณต์œ ๊ธฐ์˜ ์œ„์น˜๋กœ ๊ฐฑ์‹ ์‹œํ‚ต๋‹ˆ๋‹ค.
    • for ๋ฃจํ”„๋ฅผ ๋ชจ๋‘ ๋Œ๊ณ  ๋‚˜๋ฉด ํ˜„์žฌ ์„ค์ •ํ•œ mid๊ฐ’๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฐ„๊ฒฉ์„ ๋‘๊ณ  ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณต์œ ๊ธฐ๋ฅผ ๋ชจ๋‘ ์„ค์น˜ํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.
      • ์„ค์น˜ํ•œ ๊ณต์œ ๊ธฐ ๊ฐœ์ˆ˜(count)๊ฐ€ ์„ค์น˜ํ•ด์•ผ ํ•˜๋Š” ๊ณต์œ ๊ธฐ ๊ฐœ์ˆ˜(c)๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด ํ˜„์žฌ mid๊ฐ€ ์ •๋‹ต์ด ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ result์— mid ๊ฐ’์„ ์ €์žฅํ•˜๊ณ , ๊ฐ„๊ฒฉ์„ ๋” ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด start๋ฅผ mid + 1๋กœ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
      • ์„ค์น˜ํ•œ ๊ณต์œ ๊ธฐ ๊ฐœ์ˆ˜(count)๊ฐ€ ์„ค์น˜ํ•ด์•ผ ํ•˜๋Š” ๊ณต์œ ๊ธฐ ๊ฐœ์ˆ˜(c)๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ๊ณต์œ ๊ธฐ๋ฅผ ๋ชจ๋‘ ์„ค์น˜ํ•˜์ง€ ๋ชปํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ ๊ฐ„๊ฒฉ์„ ์ค„์—ฌ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. end๋ฅผ mid๋กœ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.

     

    ๐ŸŒฑ ์ฝ”๋“œ

    # usr/bin/env python
    # -*- coding: utf8 -*-
    # boj 2110 ๊ณต์œ ๊ธฐ ์„ค์น˜
    
    import sys
    input = sys.stdin.readline
    
    n, c = map(int, input().split())
    position = [int(input()) for _ in range(n)]
    position.sort()
    
    start,end = 1, position[n-1] - position[0]
    result = 0
    
    if c == 2:
        print(position[n - 1] - position[0])
    else:
        while(start < end):
            mid = (start + end)//2
    
            # ์ฒซ๋ฒˆ์งธ ์ง‘์—๋Š” ๋ฌด์กฐ๊ฑด ์„ค์น˜
            count = 1
            installed = position[0]
    
            for i in range(n):
                if position[i] - installed >= mid:
                    count += 1
                    installed = position[i]    # ๊ณต์œ ๊ธฐ ์„ค์น˜
            if count >= c:
                result = mid
                start = mid + 1     # ๊ฐ„๊ฒฉ ๋Š˜๋ฆฌ๊ธฐ
            elif count < c:
                end = mid       # ๊ฐ„๊ฒฉ ์ค„์ด๊ธฐ
        print(result)
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€