-
Notifications
You must be signed in to change notification settings - Fork 0
/
trains.py
118 lines (93 loc) · 2.97 KB
/
trains.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import unittest
class TestFenwickTree(unittest.TestCase):
def setUp(self):
self.t = FenwickTree(3)
def test_add_and_upto(self):
self.t.add(2)
self.assertEqual(self.t.upto(0), 0)
self.assertEqual(self.t.upto(1), 0)
self.assertEqual(self.t.upto(2), 1)
self.assertEqual(self.t.upto(3), 1)
def test_min_free(self):
self.t.add(2)
self.assertEqual(self.t.min_free(), 1)
self.t.add(1)
self.t.add(3)
self.assertEqual(self.t.min_free(), 4)
def test_remove(self):
self.t.add(2)
self.t.add(1)
self.assertEqual(self.t.min_free(), 3)
self.t.add(3)
self.t.remove(2)
self.assertEqual(self.t.min_free(), 2)
class FenwickTree:
"""Simple Fenwick tree. The keys are numbered from 1 to 2^k."""
def __init__(self, N):
while N != (N & -N):
N += N & -N
self.N = N
self.cell = [0] * (N + 1)
def add(self, key, value=1):
"""Add a value to the specified key in the tree."""
while key <= self.N:
self.cell[key] += value
key += key & -key
def remove(self, key):
"""Subtract 1 from the value of the specified key in the tree."""
self.add(key, -1)
def upto(self, key):
"""Sum the values of the keys that are smaller or equal to the given tree."""
s = 0
c = min(key, self.N)
while c:
s += self.cell[c]
c -= c & -c
return s
def min_free(self):
"""Find the smallest key for which the value is 0.
Requirement: all values are 0 or 1.
:return: The minimum free key (self.N + 1 if all keys are set).
"""
r = 1
while self.cell[r] == r:
r *= 2
if r > self.N:
return self.N + 1
b = r // 2
while b > 1:
# [r - b + 1; r] has the answer
b = b // 2
m = r - b
if self.cell[m] < b:
r = m
else:
assert self.cell[m] == b
return r
def main():
k, n = map(int, input().split())
def gen():
for train in range(n):
arrive, leave = [int(s) for s in input().split()]
yield arrive, False, train
yield leave, True, train
events = sorted(list(gen()))
# platforms are numbered from 1 to k
platforms = FenwickTree(k)
# trains are numbered from 0 to n-1
trains = []
def find_platforms():
for _, leave, train in events:
if leave:
assert train < len(trains)
platforms.remove(trains[train])
elif (platform := platforms.min_free()) <= k:
assert len(trains) == train
trains.append(platform)
platforms.add(platform)
else:
return 0, train + 1
return trains
print(*find_platforms())
if __name__ == "__main__":
main()