-
Notifications
You must be signed in to change notification settings - Fork 6
/
platform-parkour.py
51 lines (45 loc) · 1.31 KB
/
platform-parkour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright (c) 2018 kamyu. All rights reserved.
#
# Facebook Hacker Cup 2018 Round 1 - Platform Parkour
# https://www.facebook.com/hackercup/problem/1892930427431211/
#
# Time: O(N * (M + logZ))
# Space: O(N)
#
EPS = 1e-6
def check(N, H, U, D, x): # Time: O(N)
down, up = H[0]-x, H[0]+x
for i in xrange(N-1):
down, up = max(down-D[i], H[i+1]-x), min(up+U[i], H[i+1]+x)
if down > up:
return False
return True
def platform_parkour():
N, M = map(int, raw_input().strip().split())
H = [0]*N
H[0], H[1], W, X, Y, Z = map(int, raw_input().strip().split())
for i in xrange(2, N):
H[i] =(W*H[i-2]+X*H[i-1]+Y)%Z
# Time: O(N*M)
U, D = [float("inf")]*N, [float("inf")]*N
for i in xrange(M):
a, b, u, d = map(int, raw_input().strip().split())
a -= 1
b -= 1
if a > b:
a, b = b, a
u, d = d, u
for j in xrange(a, b):
U[j] = min(U[j], u)
D[j] = min(D[j], d)
# Time: O(NlogZ)
left, right = 0.0, float(max(H)-min(H))
while left+EPS < right:
mid = left + (right-left)/2
if check(N, H, U, D, mid):
right = mid
else:
left = mid
return left
for case in xrange(input()):
print 'Case #%d: %s' % (case+1, platform_parkour())