ant_count = int(input()); if ant_count < 1 | ant_count > 300_000: exit(1) ant_config = list(input()); ant_state = [] ant_collisions = {} for index, ant in enumerate(ant_config): ant_state.append({ "id": index, "direction": ant, "position": index }) ant_collisions[index] = 0 while ant_count > 1: for index, ant in enumerate(ant_state): if ant["direction"] == "L": cannotMove = any(dict_item.get("position") == ant["position"] - 1 for dict_item in ant_state) if cannotMove: ant["direction"] = "P" ant_collisions[ant["id"]] += 1 else: ant["position"] -= 1 if ant["position"] < 0: ant_state.pop(index) else: cannotMove = any(dict_item.get("position") == ant["position"] + 1 for dict_item in ant_state) if cannotMove: ant["direction"] = "L" ant_collisions[ant["id"]] += 1 else: ant["position"] += 1 if ant["position"] > len(ant_state): ant_state.pop(index) ant_count = len(ant_state) output_buffer = "" for key, value in ant_collisions.items(): output_buffer += str(value) + " " print(output_buffer)
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 | ant_count = int(input()); if ant_count < 1 | ant_count > 300_000: exit(1) ant_config = list(input()); ant_state = [] ant_collisions = {} for index, ant in enumerate(ant_config): ant_state.append({ "id": index, "direction": ant, "position": index }) ant_collisions[index] = 0 while ant_count > 1: for index, ant in enumerate(ant_state): if ant["direction"] == "L": cannotMove = any(dict_item.get("position") == ant["position"] - 1 for dict_item in ant_state) if cannotMove: ant["direction"] = "P" ant_collisions[ant["id"]] += 1 else: ant["position"] -= 1 if ant["position"] < 0: ant_state.pop(index) else: cannotMove = any(dict_item.get("position") == ant["position"] + 1 for dict_item in ant_state) if cannotMove: ant["direction"] = "L" ant_collisions[ant["id"]] += 1 else: ant["position"] += 1 if ant["position"] > len(ant_state): ant_state.pop(index) ant_count = len(ant_state) output_buffer = "" for key, value in ant_collisions.items(): output_buffer += str(value) + " " print(output_buffer) |