B - バッテリー残量 / Battery Level Editorial by admin
Qwen3-Coder-480BOverview
Given the initial battery level and per-second consumption rate of each smartphone, find the total remaining battery across all smartphones at time \(T\).
Analysis
Each smartphone \(i\) has an initial battery level \(A_i\) and consumes \(B_i\) units of battery per second. At time \(T\), the battery level would naively be \(A_i - B_i \times T\), but it cannot go negative — the minimum is \(0\).
In other words, the remaining battery of smartphone \(i\) at time \(T\) can be expressed as:
\[ \max(0, A_i - B_i \times T) \]
Therefore, we compute this for all smartphones and sum the results to get the answer.
A straightforward approach of performing this calculation for each smartphone is sufficient, and no particularly complex processing is needed. However, since the constraints are as large as \(N \leq 2 \times 10^5\) and \(T \leq 10^9\), efficient input handling is necessary. Using Python’s standard input() may result in TLE, so a faster input method (e.g., sys.stdin.read) should be used.
Algorithm
- Read all input at once (for speed).
- For each smartphone, read the initial battery level \(A_i\) and consumption rate \(B_i\), and perform the following calculation:
- Consumption at time \(T\): \(B_i \times T\)
- Remaining battery: \(\max(0, A_i - B_i \times T)\)
- Sum the remaining battery of all smartphones and output the result.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\) (excluding input data)
Implementation Notes
sys.stdin.readis used for fast input.max(0, ...)is used to ensure each smartphone’s remaining battery does not go negative.Be careful about integer overflow (\(B_i \times T\) can be on the order of \(10^{18}\), but this is not an issue in Python).
Source Code
import sys
input = sys.stdin.read
def main():
data = input().split()
N = int(data[0])
T = int(data[1])
total = 0
index = 2
for _ in range(N):
A = int(data[index])
B = int(data[index+1])
index += 2
# 消費されるバッテリー量
consumed = B * T
# 残量は0未満にならない
remaining = max(0, A - consumed)
total += remaining
print(total)
if __name__ == "__main__":
main()
This editorial was generated by qwen3-coder-480b.
posted:
last update: