公式
B - Keep the Change 解説 by en_translator
For each store, compute how much yen he lost compared to the case receiving changes.
If \(S_i=\) take, he receives the change in both cases, so he lost \(0\) yen.
If \(S_i=\) keep, he did not actually receive the change, so he lost the amount of the change. The price of the item is \(A_i\) yen and he paid \(B_i\) yen, so the change is \((B_i - A_i)\) yen. Thus, he lost \((B_i - A_i)\) yen.
Hence, the answer is
- the sum of \((B_i - A_i)\) for all stores with \(S_i=\)
keep.
This can be computed by properly receiving the input and using an if statement. The time complexity is \(\mathrm{O}(N)\), which is fast enough.
- Sample code (C++)
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
int ans = 0;
for (int i = 0; i < N; i++) {
int A, B;
cin >> A >> B;
string S;
cin >> S;
if (S == "keep") ans += B - A;
}
cout << ans << "\n";
}
投稿日時:
最終更新: