-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutility.py
More file actions
41 lines (33 loc) · 872 Bytes
/
utility.py
File metadata and controls
41 lines (33 loc) · 872 Bytes
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
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
def neighbor(N, M, y, x):
ans = []
for dy, dx in dd:
y1, x1 = y + dy, x + dx
if y1 < 0 or y1 >= N or x1 < 0 or x1 >= M:
continue
ans.append((y1, x1))
return ans
def parametric_search_max(f, left, right):
ans = 0
while left < right:
middle = (left + right) // 2
if f(middle):
ans = max(ans, middle)
left = middle + 1
else:
right = middle
if f(left):
ans = max(ans, left)
return ans
def parametric_search_min(f, left, right):
ans = int(1e9)
while left < right:
middle = (left + right) // 2
if f(middle):
ans = min(ans, middle)
right = middle
else:
left = middle + 1
if f(left):
ans = min(ans, left)
return ans