zhukov000

Binary search turtle

Nov 16th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. from math import ceil
  2. eps = 10 ** -5
  3.  
  4. vmax, d = map(int, input().split())
  5. n = int(input())
  6. x, t = [], []
  7. for i in range(n):
  8.     row = input().split()
  9.     x.append( int(row[0]) )
  10.     h, m = map(int, row[1].split(':'))
  11.     t.append( h * 60 + m )
  12.  
  13. def float_equal(a, b):
  14.     return abs(a - b) <= eps
  15.  
  16. def float_less(a, b):
  17.     return a < b and not float_equal(a, b)
  18.  
  19. def float_less_or_equal(a, b):
  20.     return float_equal(a, b) or float_less(a, b)
  21.  
  22. def time_to_last(wait_time):
  23.     time = wait_time
  24.     position = 0
  25.     cnt_eat = 0
  26.     for i in range(n):
  27.         time += (x[i] - position) / vmax
  28.         if not float_less(time, t[i]) and i < n - 1:
  29.             cnt_eat += 1
  30.             time += d
  31.         position = x[i]
  32.     return time, cnt_eat
  33.  
  34. left, right = 0, 10**9
  35. while float_less(left, right):
  36.     mid = (left + right) / 2
  37.     tlast, cnt_eat = time_to_last(mid)
  38.     if float_less_or_equal(tlast, t[-1]):
  39.         left = mid
  40.     else:
  41.         right = mid
  42.  
  43. tlast, cnt_eat = time_to_last(right)
  44. time = ceil(tlast + x[-1] / vmax + (n - cnt_eat) * d - eps)
  45. print( "%02d:%02d" % ( time // 60, time % 60 ) )
Advertisement
Add Comment
Please, Sign In to add comment