찬우의 이것저것 Chanwoo's blog

[boj 2110 python]공유기 설치

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

좌표의 범위를보고 완전탐색하면 분명히 터진다는것을 빨리 캐치해야하는것같다.

집 수 200,000

좌표 1~10,000,000,000

파라메트릭 서치를 이용하여 푼다

이를 이용하기위해 최적화문제를 결정문제로 바꿔풀어야 한다는데.. 잘 이해가 되지 않는다.

참고 링크들 정독하면서 공부를 해야할거같다.

n = 5
c = 3
arr = [1, 2, 8, 4, 9]
# n, c = map(int, input().strip().split())
# arr = [int(input()) for _ in range(n)]

arr.sort()

# mid보다 크거다 같은 간격으로 공유기를 몇개 설치할 수 있는지 return
def router_cnt(mid):
    prev = arr[0]
    cnt = 1  # 설치할 공유기 수를 담는 변수. 기본적으로 baseCase에 먼저 설치하므로 1에서 시작한다
    for i in range(1, n):
        if arr[i] - prev >= mid:  # arr[i] 의 간격이 더 넓으면 설치 가능
            cnt += 1
            prev = arr[i]
    return cnt


# 가장 인접한 공유기 사이 최대 거리를 return하는 함수
def binary_search(c):
    start = 1
    end = arr[-1] - arr[0]  # 최대 간격은 가장 큰 좌표에서 가장 작은 좌표를 뺀 것
    while start <= end:
        mid = (start + end) // 2  # mid가 간격이 된다
        router = router_cnt(mid)
        if router < c:  # 이 간격에서 설치되는 공유기의 수가 c보다 작다면
            end = mid - 1  # 간격을 좁혀야 한다
        elif router >= c:  # 크거나 같다면
            ans = mid  # 정답 후보이므로 ans에 값을 저장해두고
            start = mid + 1  # 간격을 넓혀야 한다.
    return ans


print(binary_search(c))

참고

https://sarah950716.tistory.com/16 파라매트릭 서치(개념)

https://www.crocus.co.kr/1000 파라매트릭 서치

https://jeanp-tech.github.io//articles/2020-01/boj-2110 [BOJ] 2110 : 공유기 (Python3)

https://jaimemin.tistory.com/966 백준 2110번 공유기 설치

https://claude-u.tistory.com/448 백준 파이썬 [2110] 공유기 설치 - 이분탐색

https://suri78.tistory.com/86 [백준알고리즘] 2110번: 공유기 설치 -Python

https://donggod.tistory.com/24 [2110] 공유기 설치

https://codingdog.tistory.com/entry/백준-공유기-설치-왜-이분탐색이-가능한가