We want to minimize the “slow down” steps, and hence the average delay for traffic
We can compute the optimal number of delays with a transducer that keeps track of five values
We can compute the optimal number of delays with a transducer that keeps track of five values
We can also reconstruct an optimal sequence from the table in polynomial time
red = blue = total = budget = 0
window = None
allowed = { "top": {"2O", "22", "21"},
"bottom": {"O1", "11", "21"} }
for c in columns:
cost = { "OO": 0, "1O": red, "O1": red,
"2O": blue, "O2": blue, "12": red + blue,
"11": 2*red + 1, "22": 2*blue + 1, "21": max(red + blue, 1) }[c]
if window is None and c == "21":
if blue == 0 and red >= 2:
window, budget = "top", red - 1
elif red == 0 and blue >= 2:
window, budget = "bottom", blue - 1
elif window is not None and c in allowed[window]:
cost -= 1
budget -= 1
if budget == 0:
window = None
else:
window, budget = None, 0
red = max(red + c.count("1") - 1, 0)
blue = max(blue + c.count("2") - 1, 0)
if c == "21":
red = max(red, 1)
blue = max(blue, 1)
total += cost