B - 伯爵の金庫 / The Count's Vault 解説 by admin 印刷ページ
gpt-6-astra-mediumOverview
We maintain the current number of gold coins and simulate the transactions in the given order. If there are not enough gold coins to withdraw, the count remains unchanged. After each transaction, we output the current number of coins.
Analysis
To process the next transaction, all we need is the current number of gold coins. There is no need to store all past transactions.
Letting coins be the current count, the change for each transaction is as follows:
- Deposit
D: This can always be executed, so we add \(A_i\) tocoins. - Withdrawal
W: Only if \(\mathrm{coins} \geq A_i\), we subtract \(A_i\) fromcoins. If there are not enough coins, we do nothing.
For example, consider the following transactions starting with \(5\) gold coins:
| Transaction | Action | Count after transaction |
|---|---|---|
W 3 |
Withdraw \(3\) coins | \(2\) |
W 4 |
Rejected due to insufficient coins | \(2\) |
D 6 |
Deposit \(6\) coins | \(8\) |
In this problem, a straightforward simulation is fast enough. However, unconditionally subtracting during a withdrawal will make the count negative, resulting in a wrong answer. Setting the count to \(0\) when coins are insufficient is also incorrect. When a transaction is rejected, we must keep the original count unchanged.
Algorithm
- Initialize a variable
coinsrepresenting the current number of gold coins with \(X\). - Read the \(N\) transactions one by one and perform the following:
- If \(T_i\) is
D, add \(A_i\) tocoins. - If \(T_i\) is
W, calculate the remaining count after withdrawal asafter\(= \mathrm{coins} - A_i\).- If \(\mathrm{after} \geq 0\), update
coinstoafter. - Otherwise, leave
coinsunchanged.
- If \(\mathrm{after} \geq 0\), update
- If \(T_i\) is
- Output
coinsimmediately after processing each transaction.
Initially, coins matches the actual number of gold coins. Since deposits and withdrawals are processed strictly according to the rules in the problem statement, coins will always hold the correct count after each transaction.
Complexity
- Time Complexity: \(O(N)\)
Each transaction is processed in \(O(1)\) constant time. - Space Complexity: \(O(1)\)
We do not store the transactions and only use a few variables such as the current count.
Implementation Details
A withdrawal is also possible when the number of gold coins is equal to the withdrawal amount. The condition should be
after >= 0, notafter > 0.Output is required even when a transaction is rejected. Place the output operation outside the conditional branches.
Do not output the initial \(X\); output \(N\) times in total, immediately after each transaction.
In the reference code, the count after withdrawal is calculated in a temporary variable
after, and assigned tocoinsonly when the withdrawal is possible, thus preserving the original count when coins are insufficient.Source Code
// 問題文から独立に実装: 取り出しは「引いた結果が負にならないとき」だけ実行
#include <iostream>
int main() {
int n; long long coins; std::cin >> n >> coins;
while (n--) {
char t; long long a; std::cin >> t >> a;
if (t == 'D') coins += a;
else { long long after = coins - a; if (after >= 0) coins = after; }
std::cout << coins << "\n";
}
}
This editorial was generated by gpt-6-astra-medium.
投稿日時:
最終更新: