-
Notifications
You must be signed in to change notification settings - Fork 74
/
process_packages.py
65 lines (48 loc) · 1.91 KB
/
process_packages.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
class Request:
def __init__(self, arrival_time, process_time):
self.arrival_time = arrival_time
self.process_time = process_time
class Response:
def __init__(self, dropped, start_time):
self.dropped = dropped
self.start_time = start_time
class Buffer:
def __init__(self, size):
self.size = size
self.finish_time_ = []
def Process(self, request):
if not self.finish_time_:
self.finish_time_.append(
request.arrival_time + request.process_time)
return Response(False, request.arrival_time)
while self.finish_time_ and self.finish_time_[
0] <= request.arrival_time:
self.finish_time_.pop(0)
if len(self.finish_time_) == self.size:
return Response(True, -1)
if not self.finish_time_:
self.finish_time_.append(
request.arrival_time + request.process_time)
return Response(False, request.arrival_time)
self.finish_time_.append(self.finish_time_[-1] + request.process_time)
return Response(False, self.finish_time_[-2])
def ReadRequests(count):
requests = []
for i in range(count):
arrival_time, process_time = map(int, input().strip().split())
requests.append(Request(arrival_time, process_time))
return requests
def ProcessRequests(requests, buffer):
responses = []
for request in requests:
responses.append(buffer.Process(request))
return responses
def PrintResponses(responses):
for response in responses:
print(response.start_time if not response.dropped else -1)
if __name__ == "__main__":
size, count = map(int, input().strip().split())
requests = ReadRequests(count)
buffer = Buffer(size)
responses = ProcessRequests(requests, buffer)
PrintResponses(responses)