← 返回 coinbase 的题目列表Design a Banking System with Scheduled Payments and Account Merging
类型:online_judge
Design a Banking System with Scheduled Payments and Account Merging
Implement an in-memory banking system. Every operation has a strictly increasing timestamp. Before processing an operation at time timestamp, the system must execute all scheduled payments whose execution time is no later than timestamp.
Account IDs are strings and all amounts are positive integers.
Operations
Support the following commands:
CREATE timestamp accountId
Create an account.
Fail if the account already exists.
DEPOSIT timestamp accountId amount
Deposit amount into an account.
Fail if the account does not exist.
TRANSFER timestamp fromId toId amount
Transfer amount from fromId to toId.
Both accounts must exist, must be distinct, and the sender must have sufficient funds.
A successful transfer contributes to the sender's total outgoing amount.
TOP timestamp k
Return the top k existing accounts by total outgoing amount.
Sort by total outgoing amount descending, then by account ID lexicographically ascending.
Format each entry as accountId(totalOutgoing), joined by commas.
SCHEDULE timestamp accountId amount delay
Create a scheduled payment that attempts to deduct amount from accountId at timestamp + delay.
On success, return a unique ID: payment1, payment2, and so on.
No money is deducted at scheduling time. If the balance is insufficient at execution time, the payment fails permanently.
A successfully executed payment contributes to the account's total outgoing amount.
Payments due at the same time execute in creation order.
CANCEL timestamp accountId paymentId
Cancel a scheduled payment that has not yet executed and belongs to accountId.
Executed, failed, cancelled, or foreign payments cannot be cancelled.
MERGE timestamp primaryId secondaryId
Merge secondaryId into primaryId.
Both accounts must exist and must be distinct.
After the merge:
primaryId receives both balances and both total outgoing amounts;
secondaryId no longer exists;
pending payments owned by secondaryId become owned by primaryId;
all subsequent operations on secondaryId fail.
Input Format
The first line contains an integer n, followed by n command lines.
Output Format
Print one line per command:
Print true or false for create, deposit, transfer, and merge.
Print a payment ID on successful scheduling, otherwise false.
Print true or false for cancellation.
Print the ranking result for TOP; print an empty line if no accounts exist.
Constraints
1 <= n <= 2 * 10^5
1 <= timestamp <= 10^9, and input timestamps are strictly increasing.
1 <= amount, delay <= 10^9
Account IDs and payment IDs are non-empty strings.
Example
Input
6
CREATE 1 alice
CREATE 2 bob
DEPOSIT 3 alice 100
TRANSFER 4 alice bob 30
TOP 5 2
TOP 6 1
Output
true
true
true
true
alice(30),bob(0)
alice(30)