← 返回 meta 的题目列表Design a WorkHoursRegister System (Attendance, Promotion, Salary, Grants)
类型:online_judge
Implement a WorkHoursRegister system to track contractors' office entry/exit logs, total worked hours, promotions with delayed effect, salary calculation over a time interval, and double-pay grant periods. The system evolves through 4 levels.
Assume integer timestamps ts/start/end. A work session is formed by pairing an entry time with the next exit time, represented as [in_ts, out_ts) (left-closed, right-open). Only completed sessions (those with an exit) count toward hours and salary.
Support the following APIs:
Level 1: Basic attendance
add_worker(id, pos, comp) -> bool
Add a worker with id, position pos, and hourly compensation comp.
Return False if id already exists; otherwise create and return True.
register(id, ts) -> void/bool
Toggle in/out state:
If currently out: record an entry at ts.
If currently in: record an exit at ts and close a completed session.
get(id) -> int
Return total worked hours across completed sessions only.
If the worker is currently in the office, the ongoing session does not count.
Level 2: Ranking
top_n_workers(n, pos) -> list[str]
Among workers with position pos, return the top n by total worked hours.
Sort by hours descending; tie-break by id ascending lexicographically.
Format: ["ID(Time)", ...].
Level 3: Promotion and salary within an interval
promote(id, new_pos, new_comp) -> void/bool
Register a promotion request with delayed effect:
If promoted while in-office, the current ongoing session is still paid with the old position/comp.
The new position/comp takes effect starting from the worker’s next entry.
calc_salary(id, start, end) -> int
Compute salary within [start, end].
For each completed session, compute overlap length with [start, end] and multiply by the comp applicable to that session; sum over sessions.
Level 4: Grant periods
register_grant(start, end) -> void/bool
Register a grant interval [start, end].
calc_salary(id, start, end) -> int (upgraded)
If a completed session is fully contained within any registered grant interval, that session’s pay is doubled.
Partial overlap with a grant does not qualify for doubling.
get_grant_bonus(start, end) -> int
The query must exactly match a registered grant [start, end].
Return the total “extra 1x bonus” earned by all workers within that grant, i.e., for each qualifying session fully inside the grant, add its normal (1x) pay.
Sample test scenarios
(See the 5 scenario-based examples in the Chinese section.)
Example
Input
add_worker A SDE 10
register A 1
get A
register A 6
get A
Output
True
0
5