[๋ฐฑ์ค€] ๊ณต์œ ๊ธฐ ์„ค์น˜ / 2110๋ฒˆ / ํŒŒ์ด์ฌ / python / ์ด๋ถ„ํƒ์ƒ‰
Algorithm/Baekjoon

[๋ฐฑ์ค€] ๊ณต์œ ๊ธฐ ์„ค์น˜ / 2110๋ฒˆ / ํŒŒ์ด์ฌ / python / ์ด๋ถ„ํƒ์ƒ‰

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก solutions  

๐Ÿ’ฌ ์ด๋ถ„ํƒ์ƒ‰ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ ํ•ด๊ฒฐ

๐Ÿ’ฌ start, end, mid ๋ณ€์ˆ˜๊ฐ’๋“ค์€ ๊ณต์œ ๊ธฐ ์‚ฌ์ด ๊ฑฐ๋ฆฌ๋กœ ์ •ํ•œ๋‹ค.

๐Ÿ’ฌ start๋Š” ๊ณต์œ ๊ธฐ ์‚ฌ์ด์˜ ์ตœ์†Œ ๊ฑฐ๋ฆฌ๋กœ, end๋Š” ๊ณต์œ ๊ธฐ ์‚ฌ์ด์˜ ์ตœ๋Œ€ ๊ฑฐ๋ฆฌ๋กœ ์žก๊ณ  ์ด๋ถ„ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค.

๐Ÿ’ฌ ์ง‘์˜ ์ขŒํ‘œ๋ฅผ ๋‹ด์€ house ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•œ ํ›„ ๋งจ ์ฒซ ๋ฒˆ์งธ ์ง‘๋ถ€ํ„ฐ ๊ณต์œ ๊ธฐ๋ฅผ ์„ค์น˜ํ•˜๋Š”๋ฐ ์ด๋•Œ ๊ณต์œ ๊ธฐ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ mid๋งŒํผ ์žก๋Š”๋‹ค.

๐Ÿ’ฌ ๊ณต์œ ๊ธฐ๋ฅผ ์„ค์น˜ํ•œ ํšŸ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” count๊ฐ€ ์ฃผ์–ด์ง„ ๊ณต์œ ๊ธฐ ๊ฐœ์ˆ˜ C ์ด์ƒ์ด๋ฉด ๊ณต์œ ๊ธฐ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋„“ํžˆ๊ณ  (์ด๋•Œ ๊ณต์œ ๊ธฐ ์‚ฌ์ด ์ตœ๋Œ€ ๊ฑฐ๋ฆฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” answer๊ฐ’์„ mid๋กœ ๊ฐฑ์‹ ),  C ๋ฏธ๋งŒ์ด๋ฉด ๊ฑฐ๋ฆฌ๋ฅผ ์ขํžŒ๋‹ค.

๐Ÿ‘จ‍๐Ÿ’ป code  

import sys

def binary_search(start, end):
    answer = 0
    while start <= end:
        mid = (start + end) // 2
        current = house[0]
        count = 1

        for i in range(1, len(house)):
            if house[i] >= current + mid:
                current = house[i]
                count += 1
        if count >= C:
            # ์‚ฌ์ด ๊ฑฐ๋ฆฌ ๋„“ํžˆ๊ธฐ
            start = mid + 1
            answer = mid
        else:
            # ์‚ฌ์ด ๊ฑฐ๋ฆฌ ์ขํžˆ๊ธฐ
            end = mid - 1
    return answer


input = sys.stdin.readline
N, C = map(int, input().split())
house = [int(input()) for _ in range(N)]
house.sort()

# start๋Š” ์ตœ์†Œ๊ฑฐ๋ฆฌ, end๋Š” ์ตœ๋Œ€๊ฑฐ๋ฆฌ
start = 1
end = house[-1] - house[0]

print(binary_search(start, end))

 

 

๐Ÿ“Œ problem  

 

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)๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

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




๋ฐ˜์‘ํ˜•