ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1037. Memory Management

WA5 Python
Posted by Shepeleva Elena 21 Mar 2024 22:28
class MemoryManager:
def __init__(self, n, t):
self.n = n
self.t = t
self.blocks = {}
self.free_blocks = set(range(1, n + 1))

def allocate_block(self, time):
block = min(self.free_blocks)
self.free_blocks.remove(block)
self.blocks[block] = time + self.t
return block

def access_block(self, time, block_no):
if block_no in self.blocks and self.blocks[block_no] >= time:
return '+'
else:
return '-'

n = 30000
t = 10
memory_manager = MemoryManager(n, t)

queries = input().strip()

for query in queries:
query_type = query[0]
time = query[1]
if query_type == '+':
print(memory_manager.allocate_block(time))
elif query_type == '.':
block_no = query[2]
print(memory_manager.access_block(time, block_no))