55 lines
1.1 KiB
Python
55 lines
1.1 KiB
Python
def test_distance(cows: int, locations: list[int], distance: int) -> bool:
|
|
placed_cows = 1
|
|
last = locations[0]
|
|
for location in locations[1:]:
|
|
if location - last >= distance:
|
|
last = location
|
|
placed_cows += 1
|
|
|
|
if placed_cows >= cows:
|
|
return True
|
|
|
|
return False
|
|
|
|
def main():
|
|
test_cases: int = int(input())
|
|
|
|
while test_cases != 0:
|
|
|
|
stalls, cows = [int(n) for n in input().split()]
|
|
|
|
locations = []
|
|
i = 0
|
|
while i < stalls:
|
|
|
|
locations.append(int(input()))
|
|
|
|
i += 1
|
|
|
|
locations.sort()
|
|
|
|
l = locations[1] - locations[0]
|
|
r = max(locations) - min(locations)
|
|
m = (l + r) // 2
|
|
|
|
while True:
|
|
old_m = m
|
|
|
|
# Current split worked, so try more
|
|
if test_distance(cows, locations, m):
|
|
l = m + 1
|
|
else:
|
|
r = m - 1
|
|
|
|
m = (l + r) // 2
|
|
|
|
if m == old_m:
|
|
print(m)
|
|
break
|
|
|
|
|
|
test_cases -= 1
|
|
|
|
if __name__ == '__main__':
|
|
main()
|