BOJ

[BinarySearch] 공유기 설치

YGSEO 2020. 12. 1. 02:07
728x90
n, c = list(map(int, input().split(' ')))

array = []
for _ in range(n):
    array.append(int(input()))
array.sort()

start = array[1] - array[0]
end = array[-1] - array[0]
result = 0

while (start <= end):
    mid = (start + end) // 2 # mid = the gap between two nearest routers.
    value = array[0]
    count = 1
    # install router with current mid
    for i in range(1, n):
        if array[i] >= value + mid:
            value = array[i]
            print("installing to {} as {}th router".format(array[i], count))
            count += 1
            
    if count >= c: # if more than c routers can be installed, increase range
        print("increase")
        start = mid + 1 # increase range
        result = mid # save optimal result
    else: # if not more than c, reduce range
        print("decrease")
        end = mid - 1
    print("result = {}".format(result))

print(result)

728x90