B - 伯爵の金庫 / The Count's Vault 解説 by admin 印刷ページ
gpt-6-astra-mediumOverview
Maintain the current number of gold coins in a variable and simulate the transactions in order from the beginning. Output the number of coins immediately after processing each transaction.
Analysis
To process the next transaction, all we need to know is the current number of gold coins. There is no need to keep track of past transaction history.
Let cur be the current number of coins; the change caused by each transaction is as follows:
- Deposit
D: Add \(A_i\) tocur. - Withdrawal
W:- If
curis at least \(A_i\), subtract \(A_i\) fromcur. - If
curis less than \(A_i\), do not change anything.
- If
For this problem, a naive simulation following the instructions in the problem statement is fast enough. However, if you subtract without checking the balance upon withdrawal, the number of gold coins can become negative, resulting in a wrong answer. Also, note that even if a withdrawal is declined, you still need to output the number of coins after that transaction.
For example, consider the case where you initially have \(5\) gold coins and perform the following transactions:
| Transaction | Action | Coins after transaction |
|---|---|---|
W 7 |
Insufficient coins, so make no change | \(5\) |
D 3 |
Deposit \(3\) coins | \(8\) |
W 8 |
Exactly \(8\) coins are available, so withdraw | \(0\) |
Note that if the withdrawal amount equals the current number of coins, the withdrawal is still permitted.
Algorithm
- Initialize the current number of gold coins,
cur, to the initial value \(X\). - Prepare a list
outto store the coin counts to be printed. - For each transaction, perform the following in the order of input:
- If the type is
D, add \(A_i\) tocur. - If the type is
Wandcuris at least \(A_i\), subtract \(A_i\) fromcur. - Convert the updated
curto a string and append it toout.
- If the type is
- Join the elements of
outwith newlines and output them all at once.
Initially, cur matches the actual number of gold coins. Since it is updated according to the problem rules for each transaction thereafter, the correct number of coins is always maintained. Therefore, the values recorded after each transaction represent the desired answers.
Complexity
- Time Complexity: \(O(N)\)
- Space Complexity: \(O(N)\)
Each transaction is processed in constant time. In the provided code, the output for all \(N\) transactions is stored in the list out, so the space complexity is \(O(N)\). If we only consider managing the number of gold coins, \(O(1)\) auxiliary space is sufficient.
Implementation Details
The condition to permit a withdrawal is
cur >= a. Usingcur > awill fail to correctly process transactions where the entire balance is withdrawn.When a withdrawal is declined, do not modify
cur.Place
out.append(str(cur))outside of the conditional branches so that the count is always recorded, even if a transaction is declined.What needs to be printed is the number of coins after processing each transaction. You do not need to print the initial value \(X\) before the transactions.
Source Code
import sys
def main():
data = sys.stdin.readline().split()
N = int(data[0])
cur = int(data[1])
out = []
for _ in range(N):
t, a = sys.stdin.readline().split()
a = int(a)
if t == 'D':
cur += a
else: # 'W'
if cur >= a:
cur -= a
out.append(str(cur))
sys.stdout.write("\n".join(out) + "\n")
main()
This editorial was generated by gpt-6-astra-medium.
投稿日時:
最終更新: