-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraffic_lights.py
More file actions
61 lines (46 loc) · 1.27 KB
/
traffic_lights.py
File metadata and controls
61 lines (46 loc) · 1.27 KB
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
60
61
import sys
# Fast I/O
input = sys.stdin.readline
# Common imports
from collections import defaultdict, deque, Counter
from itertools import combinations, permutations, product, accumulate
from functools import lru_cache, reduce
import heapq
import bisect
# Constants
MAX = float('inf')
MIN = float('-inf')
MOD = 10**9 + 7
def solve():
x, n = map(int, input().split())
positions = list(map(int, input().split()))
# Sorted list of traffic light positions
lights = [0, x]
# Multiset of segment lengths
segments = Counter()
segments[x] = 1
max_len = x
result = []
for p in positions:
# Find position to insert
idx = bisect.bisect_left(lights, p)
left = lights[idx - 1]
right = lights[idx]
# Remove old segment
old_len = right - left
segments[old_len] -= 1
if segments[old_len] == 0:
del segments[old_len]
# Add new segments
left_len = p - left
right_len = right - p
segments[left_len] += 1
segments[right_len] += 1
# Insert new light
lights.insert(idx, p)
# Update maximum segment length
max_len = max(segments)
result.append(max_len)
print(*result)
if __name__ == "__main__":
solve()