Advertisement
PowerCell46

Truck Tour

Apr 30th, 2023
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. number_of_petrol_stations = int(input())  # Number of petrol stations on the track
  2. indexes = []  # Possible valid indexes
  3. dict = {}  # Keeping the values for later checking
  4. for i in range(0, number_of_petrol_stations):
  5.     petrol_fuel, kilometers = [int(num) for num in input().split(" ")]
  6.     dict[i] = {}
  7.     dict[i][petrol_fuel] = kilometers  # Adding to the dictionary the current values fuel and kilometers
  8.     if petrol_fuel > kilometers:
  9.         indexes.append(i)  # if you could start from this station, append it to the list for possible starting point
  10.  
  11. for index in indexes:
  12.     current_dict = dict.copy()  # Making a current dictionary copy which we can change
  13.     left_out_fuel = list(dict[index].keys())[0] - list(dict[index].values())[0]  # Calculating how much more fuel we have in the tank
  14.     del current_dict[index]
  15.     xedni = index
  16.     this_is_the_index = True
  17.     while len(current_dict) > 0: # With this cycle we move through all the stations
  18.         xedni += 1
  19.         if xedni not in current_dict.keys():
  20.             xedni = 0
  21.         current_fuel = list(current_dict[xedni].keys())[0]
  22.         current_kilometeres = list(current_dict[xedni].values())[0]
  23.         current_left_out_fuel = current_fuel - current_kilometeres
  24.         if left_out_fuel + current_left_out_fuel < 0: # If we subtract the current needed fuel from the fuel we have and we have a negative number, we stop the cycle and continue with the next number
  25.             this_is_the_index = False
  26.             break
  27.         left_out_fuel += current_left_out_fuel
  28.         del current_dict[xedni]
  29.     if this_is_the_index: # If we haven't stopped the cycle, this means that the current number is eligible to be a starting point, and the first one gets printed and the program stops
  30.         print(index)
  31.         break
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement