백준 2110번 공유기 설치

DARTZ·2022년 5월 16일
0

알고리즘

목록 보기
59/135
import sys
sys.stdin = open('input.txt', 'rt')
input = sys.stdin.readline

N, C = map(int, input().split())

house = []

for _ in range(N):
    house.append(int(input()))

house.sort() # 위치대로 정렬은 필수. 왼쪽부터 채워 넣는 것이 최대 값이 나오므로..

start = 1
end = max(house)

while start <= end:
    middle = (start + end) // 2
    standard = house[0] + middle # 범위내 탐색을 위해서 설정
    count = 1

    for h in house: # house의 원소를 탐색하면서 standard랑 같거나 넘을 때,
    				# count에 1을 더한다.
        if h >= standard:
            count += 1
            standard = h + middle

    if count >= C:
        answer = middle
        start = middle + 1

    else:
        end = middle - 1

print(answer)
profile
사람들이 비용을 지불하고 사용할 만큼 가치를 주는 서비스를 만들고 싶습니다.

0개의 댓글