← 返回 ramp 的题目列表Banking System with Payments and Account Merging
类型:online_judge
Problem: Implement a Banking System
Implement a simplified banking system that processes operations in non-decreasing timestamp order. The system supports account creation, deposits, transfers, payments with cashback, payment-status queries, account merging, and top-spender queries.
Read operations from standard input and print the result of each operation line by line.
Input Format
The first line contains an integer Q, the number of operations.
Each of the next Q lines is one operation:
CREATE timestamp accountId
DEPOSIT timestamp accountId amount
TRANSFER timestamp sourceAccountId targetAccountId amount
PAY timestamp accountId amount
GET_PAYMENT_STATUS timestamp accountId paymentId
MERGE timestamp accountId1 accountId2
TOP_SPENDERS timestamp n
timestamp is an integer. All timestamps are non-decreasing.
accountId is a string without spaces.
amount is a non-negative integer. For TRANSFER and PAY, the amount must be positive to succeed.
Operations
1. CREATE timestamp accountId
Create an account with balance 0.
If the account does not currently exist, output true.
If the account already exists and is active, output false.
2. DEPOSIT timestamp accountId amount
Deposit amount into an account.
If the account exists and is active, output the new balance.
Otherwise, output -1.
3. TRANSFER timestamp sourceAccountId targetAccountId amount
Transfer amount from sourceAccountId to targetAccountId.
The transfer succeeds only if:
both accounts exist and are active;
the two accounts are different;
the source account has at least amount balance;
amount > 0.
On success:
subtract amount from the source account;
add amount to the target account;
add amount to the source account's total outgoing amount;
output the source account's remaining balance.
On failure, output -1.
4. PAY timestamp accountId amount
Pay amount from an account and generate a payment id.
The payment succeeds only if:
the account exists and is active;
the account has at least amount balance;
amount > 0.
On success:
subtract amount from the account balance;
add amount to the account's total outgoing amount;
generate a payment id: payment1, payment2, ... globally increasing by successful payments;
schedule cashback at timestamp + 86400000, with cashback amount floor(amount * 2 / 100);
output the payment id.
On failure, output an empty string, i.e. a blank line.
Before processing any operation, apply all pending cashbacks whose scheduled time is <= current timestamp.
5. GET_PAYMENT_STATUS timestamp accountId paymentId
Query a payment's status.
If the account does not exist or is inactive, output an empty string.
If the payment id does not exist, output an empty string.
If the payment does not belong to the current account, output an empty string.
If cashback has not been received yet, output IN_PROGRESS.
If cashback has been received, output CASHBACK_RECEIVED.
If the payment's original account was later merged into another account, the payment belongs to the merged active account.
6. MERGE timestamp accountId1 accountId2
Merge accountId2 into accountId1.
The merge succeeds only if:
both accounts exist and are active;
the two accounts are different.
On success:
add accountId2's balance to accountId1;
add accountId2's total outgoing amount to accountId1;
pending cashbacks from accountId2 should later be credited to accountId1;
historical payments from accountId2 now belong to accountId1;
accountId2 becomes inactive and cannot be used directly anymore.
Output true on success, otherwise false.
7. TOP_SPENDERS timestamp n
Return the top n active accounts by total outgoing amount.
Sorting rules:
Higher total outgoing amount comes first.
If tied, lexicographically smaller accountId comes first.
Output format:
accountId1(totalOutgoing1), accountId2(totalOutgoing2)
If there are fewer than n active accounts, return all active accounts.
Constraints
1 <= Q <= 200000
1 <= len(accountId) <= 50
0 <= timestamp <= 10^18
0 <= amount <= 10^12
1 <= n <= 100000
Example
Input
10
CREATE 1 A
CREATE 2 B
DEPOSIT 3 A 1000
TRANSFER 4 A B 300
PAY 5 B 200
GET_PAYMENT_STATUS 6 B payment1
MERGE 7 A B
GET_PAYMENT_STATUS 8 A payment1
TOP_SPENDERS 9 2
DEPOSIT 86400005 A 0
Output
true
true
1000
700
payment1
IN_PROGRESS
true
IN_PROGRESS
A(500)
804
Example
Input
6
CREATE 1 A
CREATE 2 B
DEPOSIT 3 A 100
TRANSFER 4 A B 30
TRANSFER 5 A C 10
TOP_SPENDERS 6 2
Output
true
true
100
70
-1
A(30), B(0)