B - 試験対策 / Exam Preparation 解説 by admin
Qwen3-Coder-480BOverview
Given each subject’s current score and the study time required to raise it by one point, find the minimum total study time needed to bring all subjects up to at least the passing score.
Analysis
In this problem, for each subject, studying is only necessary when the current score falls below the passing score \(T\). For example, if a subject’s current score is \(A_i\) and the passing score is \(T\), the number of points needed is \(\max(0, T - A_i)\). Furthermore, since it takes \(C_i\) hours to raise the score by one point, the study time required for that subject is \((T - A_i) \times C_i\) hours.
A naive approach might be to simulate raising each subject’s score one point at a time, but this is wasteful and computationally inefficient (especially since the maximum score can be up to \(10^4\)). However, since the required study time for each subject can be calculated directly, the problem can be solved by simply summing up the times across all subjects.
Therefore, we can ignore subjects that already pass (\(A_i \geq T\)) and only compute and sum the required time for failing subjects.
Algorithm
- For each subject, read the current score \(A_i\) and the study time coefficient \(C_i\).
- If \(A_i < T\), add \((T - A_i) \times C_i\) to the total study time.
- After processing all subjects, output the total study time.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\)
Implementation Notes
- Since each subject only needs to be processed once, calculations can be performed immediately while reading input.
- Instead of using unnecessary arrays or lists, only the running total is maintained to save memory.
## Source Code
```python
N, T = map(int, input().split())
total_time = 0
for _ in range(N):
A, C = map(int, input().split())
if A < T:
total_time += (T - A) * C
print(total_time)
This editorial was generated by qwen3-coder-480b.
投稿日時:
最終更新: