A - ポイントカードの残高管理 / Point Card Balance Management 解説 by admin
Qwen3-Coder-480BOverview
This problem requires starting from initial point balances for each member, sequentially applying specified adjustment operations, and determining the final balances.
Analysis
In this problem, we need to manage each member’s point balance and apply multiple update operations. Point changes are simple additions, but since the balance is not allowed to go negative, if the value becomes negative after addition, it must be corrected to \(0\).
A straightforward approach is to directly update the array values for each operation. This works without any issues. Looking at the constraints, both \(N\) and \(M\) are at most \(10^5\), and each operation can be processed in constant time, so the overall solution runs sufficiently fast. If the operations were more complex or required range updates, different techniques would be needed, but since each operation in this problem is a single-point update, we can simply simulate the process.
For example, suppose the initial points are [10, 20, 30] and an operation of -25 is applied to member 2.
The calculation gives \(20 + (-25) = -5\), but this is corrected to \(0\).
Performing such operations one by one in a straightforward manner is the optimal approach.
Algorithm
- Store each member’s initial points in a list
points. - Repeat the following for \(M\) point adjustment operations:
- Read the target member number \(T\) and the value to add \(V\).
- Since \(T\) is 1-indexed, convert it to 0-indexed to correspond to the list index.
- Add \(V\) to
points[T - 1]. - If the value after addition is negative, set it to \(0\).
- Output the final contents of
pointsseparated by spaces.
Complexity
- Time complexity: \(O(N + M)\)
- Space complexity: \(O(N)\)
Reading the initial values takes \(O(N)\), and processing each operation takes \(O(1)\) for a total of \(O(M)\). Combined, this gives \(O(N + M)\). The space complexity is \(O(N)\) for the list that stores the point information.
Implementation Notes
- Since member numbers are given as 1-indexed, don’t forget to convert to 0-indexed internally.
- Use an
ifstatement to check that points do not become negative after addition. - For the final output, using
print(*points)allows concise space-separated output.
## Source Code
```python
# 入力の読み込み
N, M = map(int, input().split())
points = list(map(int, input().split()))
# ポイント調整処理
for _ in range(M):
T, V = map(int, input().split())
index = T - 1 # 0-indexedにする
points[index] += V
if points[index] < 0:
points[index] = 0
# 結果の出力
print(*points)
This editorial was generated by qwen3-coder-480b.
投稿日時:
最終更新: