Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import sys
  2.  
  3. def read_input():
  4. return list(map(int, sys.stdin.read().split()))
  5.  
  6. # Tries to reach the destination
  7. # - if destination reachable, returns number of stops
  8. # - otherwise: returns -1
  9. def drive(distance, tank_capacity, stops):
  10. tank = tank_capacity
  11. distance_travelled = 0
  12. stops_made = 0
  13.  
  14. if tank_capacity < distance - stops[-1]:
  15. fail(-1)
  16. # For every stop we are about to move to
  17. for stop in stops:
  18. if tank >= stop - distance_travelled: # if we have enough fuel to reach the next stop
  19. tank, distance_travelled = move(stop, tank, distance_travelled)
  20. # print("Move: tank: {}, distance_travelled: {}".format(tank, distance_travelled))
  21. continue
  22. else:
  23. if tank_capacity >= stop - distance_travelled: # if refilling will help us reach the next stop
  24. # print("Refilling: stop: {}, distance_travelled: {}".format(stop, distance_travelled))
  25. tank = tank_capacity # refill
  26. stops_made += 1
  27. tank, distance_travelled = move(stop, tank, distance_travelled)
  28. else:
  29. fail(stop)
  30. if tank == 0 and distance != stop: # if our tank is empty and we need to move
  31. stops_made += 1 # refill
  32. return stops_made
  33.  
  34. def move(stop, tank, distance_travelled):
  35. distance_to_travel = stop - distance_travelled
  36. return (tank - distance_to_travel, stop)
  37.  
  38. def fail(stop):
  39. print("Give up: stop: {}".format(stop))
  40. return -1
  41.  
  42. if __name__ == "__main__":
  43. data = read_input()
  44. # data[2] - skipping, we don't care about the number of stops
  45. distance, tank_capacity, stops = data[0], data[1], data[3:]
  46. stops_made = drive(distance, tank_capacity, stops)
  47. print(stops_made)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement