-
Notifications
You must be signed in to change notification settings - Fork 0
/
t6.py
41 lines (31 loc) · 945 Bytes
/
t6.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
import sys
nsect = int(input())
npart = int(input())
lines = sys.stdin.readlines()
partitions = [] # we have at most 1000 partitions, so just put them in a list
for line in lines:
a, b = line.split()
a = int(a)
b = int(b)
assert a <= b, f"err: ({a=}) > ({b=})"
partitions.append((a, b))
def partitions_overlap(partitions):
""" Count overlapped partitions, inserted in a certain order.
Return count.
"""
arr = [] # (start, end) tuples
for ax, bx in partitions:
ileft, iright = 0, len(arr) #
for i, (ai, bi) in enumerate(arr):
if bx < ai:
iright = i
break
elif ax > bi:
ileft = i + 1
else: # overlap
pass
arr = arr[:ileft] + [(ax,bx)] + arr[iright:]
n_part = len(arr)
return n_part
n_part = partitions_overlap(partitions)
print(n_part)