def count_collisionsv2(n, directions): # Inicjalizacja listy wynikowej, w której będziemy przechowywać liczbę odbić dla każdej mrówki collisions = [0] * n l2r = [0] * n r2l = [0] * n prev_colls = 0 prev_sym = "L" for i in range(n): if directions[i] == "P": if prev_sym == "P": prev_colls += 2 else: prev_colls += 1 else: if prev_sym == "P": prev_colls += 1 else: prev_colls += 0 l2r[i] = prev_colls prev_sym = directions[i] prev_colls = 0 prev_sym = "P" for i in reversed(range(n)): if directions[i] == "L": if prev_sym == "L": prev_colls += 2 else: prev_colls += 1 else: if prev_sym == "L": prev_colls += 1 else: prev_colls += 0 r2l[i] = prev_colls prev_sym = directions[i] for i in range(n): collisions[i] = min(l2r[i], r2l[i]) return collisions if __name__ == '__main__': # Wczytanie danych wejściowych n = int(input()) directions = list(input().strip()) # Obliczenie liczby odbić dla każdej mrówki result = count_collisionsv2(n, directions) # Wyświetlenie wyniku print(*result)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | def count_collisionsv2(n, directions): # Inicjalizacja listy wynikowej, w której będziemy przechowywać liczbę odbić dla każdej mrówki collisions = [0] * n l2r = [0] * n r2l = [0] * n prev_colls = 0 prev_sym = "L" for i in range(n): if directions[i] == "P": if prev_sym == "P": prev_colls += 2 else: prev_colls += 1 else: if prev_sym == "P": prev_colls += 1 else: prev_colls += 0 l2r[i] = prev_colls prev_sym = directions[i] prev_colls = 0 prev_sym = "P" for i in reversed(range(n)): if directions[i] == "L": if prev_sym == "L": prev_colls += 2 else: prev_colls += 1 else: if prev_sym == "L": prev_colls += 1 else: prev_colls += 0 r2l[i] = prev_colls prev_sym = directions[i] for i in range(n): collisions[i] = min(l2r[i], r2l[i]) return collisions if __name__ == '__main__': # Wczytanie danych wejściowych n = int(input()) directions = list(input().strip()) # Obliczenie liczby odbić dla każdej mrówki result = count_collisionsv2(n, directions) # Wyświetlenie wyniku print(*result) |