summaryrefslogtreecommitdiff
path: root/6_2023.py
blob: cb8b279cf6f390712f4fdc1b7a77f60cb697e0ca (plain)
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
52
53
54
55
56
57
58
59
import sys
import re
from collections import defaultdict

D = open(sys.argv[1]).read().strip()
L = D.split("\n")
ans = 0

t, d = L
times = [int(x) for x in t.split(":")[1].split()]
dist = [int(x) for x in d.split(":")[1].split()]

T = ""
for t in times:
    T += str(t)
T = int(T)
D = ""
for d in dist:
    D += str(d)
D = int(D)


def f(t, d):
    # runs in O(log(t))
    # let g(x) = x*(t-x) is maximized at t//2
    # we want to know: what is the lowest value s.t. g(x) >= d
    # we want to know: what is the highest value s.t. g(x) >= d
    def g(x):
        return x * (t - x)

    lo = 0
    hi = t // 2
    if hi * (t - hi) < d:
        return 0
    assert g(lo) < d and g(hi) >= d
    while lo + 1 < hi:
        m = (lo + hi) // 2
        if g(m) >= d:
            hi = m
        else:
            lo = m
    assert lo + 1 == hi
    assert g(lo) < d and g(hi) >= d
    first = hi
    assert g(first) >= d and g(first - 1) < d

    # g(x) == g(t-x), so there's symmetry about the midpoint t/2
    last = int((t / 2) + (t / 2 - first))
    assert (
        g(last) >= d and g(last + 1) < d
    ), f"last={last} g(last)={g(last)} {g(last+1)} d={d}"
    return last - first + 1


ans = 1
for i in range(len(times)):
    ans *= f(times[i], dist[i])
print(ans)
print(f(T, D))