Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python 3.7.4
- # Function to simulate the process of finding the lords of the ring
- def find_lords_of_the_ring(n, potential):
- changes = True # Flag to check if there were changes in the last iteration
- while changes: # While there were changes in the last iteration
- n = len(potential) # Update the size of the list
- rem = [False] * n # List to mark the elements to remove in this iteration
- changes = False # Reset the flag
- for i in range(n - 1):
- if potential[i] >= 0 and potential[i + 1] < 0: # If facing in different directions
- if abs(potential[i]) == abs(potential[i + 1]): # If they have the same potential, remove both
- rem[i] = True
- rem[i + 1] = True
- elif abs(potential[i]) > abs(potential[i + 1]): # If the potential of the first is greater, remove the second
- rem[i + 1] = True
- else: # If the potential of the second is greater, remove the first
- rem[i] = True
- changes = True # There were changes in this iteration
- if not changes: # If there were no changes in this iteration,
- break
- new_potential = [] # List to store the new potential
- for i in range(n):
- if not rem[i]:
- new_potential.append(potential[i])
- potential = new_potential # Update the potential
- return potential
- if __name__ == "__main__":
- # Read input
- n = int(input())
- potential = list(map(int, input().split()))
- # Find the answer and print it
- for p in find_lords_of_the_ring(n, potential):
- print(p, end=" ")
- print("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement