← 返回 bytedance 的题目列表Aligned Memory Allocator with Alloc/Erase Operations
类型:online_judge
Problem: 8-Byte Aligned Memory Allocator (alloc / erase)
You are given a memory of length n represented by an array:
mem[i] = 0 means the cell is free
mem[i] = 1 means the cell is occupied initially
Process a sequence of operations:
alloc x
Allocation must start at an index that is a multiple of 8.
Among all valid start indices, find the leftmost start s such that the segment [s, s+x-1] is entirely free.
If found, mark the segment as occupied and assign it a unique positive integer ID (starting from 1, increasing by 1 per successful alloc). Output the start index s.
If not found, output -1.
erase id
Free all cells that were allocated with this id (set them back to free).
Output the number of freed cells; if id does not exist, output -1.
Constraints / Notes
Start index must satisfy s % 8 == 0.
Allocation must not go out of bounds.
Initial occupied cells do not belong to any alloc ID.
Input (one common OA format)
Line 1: integer n
Line 2: n space-separated 0/1 integers
Line 3: integer q
Next q lines: operations alloc x or erase id
Output
One integer per operation (the return value).
Example
Input
16
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3
alloc 8
alloc 8
erase 1
Output
0
8
8