B - 伯爵の金庫 / The Count's Vault 解説 by admin 印刷ページ
gpt-6-astra-mediumOverview
We keep track of the current number of gold coins and simulate the transactions one by one from the beginning. After processing each transaction, we output the current number of coins.
Analysis
All that is needed to process the next transaction is the current number of gold coins. There is no need to store all past transactions.
Let \(x\) be the current number of coins and \(a\) be the number of coins in the transaction. The operations are as follows:
- Deposit
D: Add \(a\) to \(x\). - Withdrawal
W:- If \(x \geq a\), subtract \(a\) from \(x\).
- If \(x < a\), the transaction is declined, so \(x\) remains unchanged.
For this problem, a straightforward simulation as described in the problem statement is sufficiently fast. However, unconditionally subtracting \(a\) for each withdrawal would lead to incorrect answers in the case of insufficient funds. Furthermore, when there are insufficient funds, we must not set the number of coins to \(0\), but rather keep the number of coins before the transaction as it is.
For example, consider the case where there are initially \(5\) gold coins and the following transactions occur:
| Transaction | Action | Coins after transaction |
|---|---|---|
D 3 |
Deposit \(3\) coins | \(8\) |
W 10 |
Not enough coins, do nothing | \(8\) |
W 8 |
Exactly \(8\) coins available, withdraw them | \(0\) |
We output the number of coins after each transaction, including when a transaction is declined.
Algorithm
- Read \(N\) and \(X\), and initialize the current number of gold coins \(x\) with \(X\).
- Repeat the following \(N\) times:
- Read the transaction type \(t\) and the amount \(a\).
- If \(t\) is
D, set \(x \leftarrow x+a\). - Otherwise, if \(a \leq x\), set \(x \leftarrow x-a\).
- Output the current value of \(x\).
Initially, \(x\) matches the actual number of gold coins. Since \(x\) is updated for each transaction according to the rules in the problem statement, it always correctly represents the contents of the vault. Therefore, outputting \(x\) after each transaction gives the required answers.
Complexity
- Time complexity: \(O(N)\)
Each transaction is processed in constant time. - Space complexity: \(O(1)\)
Since transactions are read and processed one by one, we only need to store a constant number of variables, such as the current number of coins.
Implementation Details
Since the amount
aread byinput().split()is a string, convert it to an integer usingint(a).The condition for being able to withdraw is
a <= x. A withdrawal is also allowed when the balance is equal to the withdrawal amount.Since the only transaction types are
DandW,elif a <= x:followingif t == 'D':serves as the conditional check for withdrawals.Place
print(x)outside of the conditional branches. This ensures that the output is produced even if the withdrawal is declined.Source Code
# 問題文から独立に実装
n, x = map(int, input().split())
for _ in range(n):
t, a = input().split()
a = int(a)
if t == 'D':
x += a
elif a <= x:
x -= a
print(x)
This editorial was generated by gpt-6-astra-medium.
投稿日時:
最終更新: