Advertisement
Sekai02

Lord of the Rings (python)

Nov 25th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. # Python 3.7.4
  2.  
  3. # Function to simulate the process of finding the lords of the ring
  4. def find_lords_of_the_ring(n, potential):
  5.     changes = True  # Flag to check if there were changes in the last iteration
  6.     while changes:  # While there were changes in the last iteration
  7.         n = len(potential)  # Update the size of the list
  8.         rem = [False] * n   # List to mark the elements to remove in this iteration
  9.         changes = False     # Reset the flag
  10.  
  11.         for i in range(n - 1):
  12.             if potential[i] >= 0 and potential[i + 1] < 0:  # If facing in different directions
  13.                 if abs(potential[i]) == abs(potential[i + 1]):  # If they have the same potential, remove both
  14.                     rem[i] = True
  15.                     rem[i + 1] = True
  16.                 elif abs(potential[i]) > abs(potential[i + 1]):  # If the potential of the first is greater, remove the second
  17.                     rem[i + 1] = True
  18.                 else:  # If the potential of the second is greater, remove the first
  19.                     rem[i] = True
  20.  
  21.                 changes = True  # There were changes in this iteration
  22.  
  23.         if not changes:  # If there were no changes in this iteration,
  24.             break
  25.  
  26.         new_potential = []  # List to store the new potential
  27.         for i in range(n):
  28.             if not rem[i]:
  29.                 new_potential.append(potential[i])
  30.  
  31.         potential = new_potential  # Update the potential
  32.  
  33.     return potential
  34.  
  35.  
  36. if __name__ == "__main__":
  37.     # Read input
  38.     n = int(input())
  39.     potential = list(map(int, input().split()))
  40.  
  41.     # Find the answer and print it
  42.     for p in find_lords_of_the_ring(n, potential):
  43.         print(p, end=" ")
  44.     print("\n")
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement