last lab
This commit is contained in:
66
labPython/lab7/BinSearch_2.py
Executable file
66
labPython/lab7/BinSearch_2.py
Executable file
@@ -0,0 +1,66 @@
|
||||
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}")
|
||||
Reference in New Issue
Block a user