← 返回 ramp 的题目列表Multi-Level Inventory Storage with Expiration and Retrieval Priority
类型:online_judge
Problem: Multi-Level Inventory Storage System
Implement an InventorySystem class that supports initialization, storing items, and retrieving items.
The warehouse has N levels, numbered from 0 to N - 1, where level 0 is the top level. Each level has the same weight capacity capacity.
Each item contains:
item_id: a globally unique string;
level: the level where the item should be stored;
weight: a positive integer weight;
timestamp: the time when the item is stored;
ttl: time-to-live. The item is considered expired at time timestamp + ttl and afterwards.
Implement:
class InventorySystem:
def __init__(self, n: int, capacity: int):
pass
def store(self, item_id: str, level: int, weight: int, timestamp: int, ttl: int) -> bool:
pass
def retrieve(self, timestamp: int) -> str:
pass
store Rules
When calling store(item_id, level, weight, timestamp, ttl):
Before the operation, remove all items that are already expired at timestamp;
If level is out of range, return False;
If an active item with the same item_id already exists, return False;
If the target level does not have enough remaining capacity, return False;
Otherwise, store the item in the specified level and return True.
retrieve Rules
When calling retrieve(timestamp):
Before the operation, remove all items that are already expired at timestamp;
Scan levels from top to bottom, i.e. from 0 to N - 1;
A level is eligible for retrieval only if its remaining capacity is at least 50% of its total capacity;
From the first eligible level that contains non-expired items, retrieve the item with the largest weight;
If multiple items have the same weight, choose the one with the earlier timestamp; if still tied, choose the lexicographically smaller item_id;
Remove the retrieved item from the system and return its item_id;
If no item can be retrieved, return "EMPTY".
Input Format
For judging purposes, the input is a sequence of commands:
n capacity
q
command_1
command_2
...
command_q
Commands are one of:
STORE item_id level weight timestamp ttl
RETRIEVE timestamp
Output Format
Print one line for each command:
For STORE, print STORED if successful, otherwise print REJECTED;
For RETRIEVE, print the retrieved item_id, or EMPTY if none exists.
Constraints
1 <= n <= 10^3
1 <= capacity <= 10^9
1 <= q <= 2 * 10^5
1 <= weight <= capacity
1 <= ttl <= 10^9
Command timestamps are non-decreasing
item_id contains only letters, digits, and underscores, and has length at most 32
Example
Input:
2 100
5
STORE A 0 30 0 10
STORE B 0 20 1 10
RETRIEVE 2
RETRIEVE 3
RETRIEVE 12
Output:
STORED
STORED
A
B
EMPTY
Example
Input
2 100
5
STORE A 0 30 0 10
STORE B 0 20 1 10
RETRIEVE 2
RETRIEVE 3
RETRIEVE 12
Output
STORED
STORED
A
B
EMPTY