Files
labs/labPython/lab7/BinSearch_2.py
2026-04-18 14:42:00 +05:00

67 lines
1.6 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
def find_min_radius(feasible, eps=1e-6, max_radius=None):
if max_radius is None:
high_bound = 1.0
while not feasible(high_bound):
high_bound *= 2
if high_bound > 1e10:
raise ValueError(
"Не удалось найти верхнюю границу. Возможно, feasible всегда возвращает False."
)
low = 0.0
high = high_bound
else:
low = 0.0
high = max_radius
while high - low > eps:
mid = (low + high) / 2.0
if feasible(mid):
high = mid
else:
low = mid
return high
def can_cover(points, m, r):
if not points:
return True
used_circles = 0
i = 0
while i < len(points):
circle_center = points[i] + r
circle_right = circle_center + r
used_circles += 1
if used_circles > m:
return False
while i < len(points) and points[i] <= circle_right:
i += 1
return True
if __name__ == "__main__":
print("Введите N и M через пробел:")
n, m = map(int, input("> ").split())
print(f"Введите {n} координат точек (по одной на строке):")
points = []
for _ in range(n):
point = float(input().strip())
points.append(point)
points.sort()
def feasible(r):
return can_cover(points, m, r)
eps = 1e-6
result = find_min_radius(feasible, eps=eps)
print(f"Минимальный радиус: {result:.6f}")