summaryrefslogtreecommitdiff
path: root/sources
diff options
context:
space:
mode:
Diffstat (limited to 'sources')
-rw-r--r--sources/network_packet.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/sources/network_packet.py b/sources/network_packet.py
new file mode 100644
index 0000000..46ac2fb
--- /dev/null
+++ b/sources/network_packet.py
@@ -0,0 +1,63 @@
+# python3
+
+from collections import namedtuple, deque
+
+Request = namedtuple("Request", ["arrived_at", "time_to_process"])
+Response = namedtuple("Response", ["was_dropped", "started_at"])
+
+
+class Buffer:
+ def __init__(self, capacity):
+ self.capacity = capacity
+ self.finish_time = deque()
+ self.running_time_counter = 0
+
+ def process(self, request):
+ if self.running_time_counter < request.arrived_at:
+ if len(self.finish_time) == 0:
+ self.running_time_counter = request.arrived_at
+ elif self.finish_time[-1] < request.arrived_at:
+ self.running_time_counter = request.arrived_at
+
+ while len(self.finish_time) > 0:
+ j = self.finish_time[0]
+ if j <= request.arrived_at:
+ self.finish_time.popleft()
+ else:
+ break
+
+ if len(self.finish_time) >= self.capacity:
+ return Response(True, -1)
+
+ response = Response(False, self.running_time_counter)
+ self.running_time_counter = self.running_time_counter + request.time_to_process
+ if len(self.finish_time) > 0:
+ self.finish_time.append(self.finish_time[-1] + request.time_to_process)
+ else:
+ self.finish_time.append(self.running_time_counter)
+ return response
+
+
+def process_requests(requests, buffer):
+ responses = []
+ for request in requests:
+ responses.append(buffer.process(request))
+ return responses
+
+
+def main():
+ buffer_size, n_requests = map(int, input().split())
+ requests = []
+ for _ in range(n_requests):
+ arrived_at, time_to_process = map(int, input().split())
+ requests.append(Request(arrived_at, time_to_process))
+
+ buffer = Buffer(buffer_size)
+ responses = process_requests(requests, buffer)
+
+ for response in responses:
+ print(response.started_at if not response.was_dropped else -1)
+
+
+if __name__ == "__main__":
+ main()