Created
June 12, 2023 13:15
-
-
Save Reagent992/5c55fb80c87fd4a606a8cfb302417059 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Задача № 1. | |
A. Ближайший ноль | |
№ Посылки в Яндекс Контесте: 88140410 | |
""" | |
def distance_to_zero(amount: int, array: list) -> tuple[list, list]: | |
"""Подсчет расстояний до нуля.""" | |
left_dist = [0] * amount | |
right_dist = [0] * amount | |
# Вычисляем расстояние до ближайшего нуля слева направо | |
dist = float('+inf') # Бесконечно большое число - заглушка. | |
for i in range(amount): | |
if array[i] == 0: | |
dist = 0 | |
else: | |
dist += 1 | |
left_dist[i] = dist | |
# Вычисляем расстояние до ближайшего нуля справа налево | |
dist = float('+inf') | |
for i in reversed(range(amount)): | |
if array[i] == 0: | |
dist = 0 | |
else: | |
dist += 1 | |
right_dist[i] = dist | |
return left_dist, right_dist | |
def choose_min(left_dist: list, right_dist: list, amount: int) -> list: | |
"""Выбираем минимальное расстояние из двух списков.""" | |
result = [0] * amount | |
for i in range(amount): | |
result[i] = min(left_dist[i], right_dist[i]) | |
return result | |
def main(amount: int, array: list) -> None: | |
"""Основная функция.""" | |
left_dist, right_dist = distance_to_zero(amount, array) | |
result = choose_min(left_dist, right_dist, amount) | |
print(' '.join(map(str, result))) | |
amount = int(input()) # Количество домов. | |
array = list(map(int, input().split())) # Список номеров домов. | |
main(amount, array) # Запуск основной функции. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment