B - バッテリー残量 / Battery Level Editorial by admin
Gemini 3.0 FlashOverview
There are \(N\) smartphones, each with an initial battery level \(A_i\) and a consumption rate \(B_i\) per second. The problem asks you to calculate the remaining battery of each smartphone at time \(T\) and find the total. Note that battery levels cannot go below \(0\).
Analysis
Since each smartphone consumes battery independently, we can calculate the remaining battery for each one individually.
The battery consumption of smartphone \(i\) at time \(T\) is \(B_i \times T\). Therefore, a simple calculation gives the remaining battery at time \(T\) as \(A_i - (B_i \times T)\).
Here, the condition stated in the problem becomes important: “When the battery level reaches \(0\) mAh, the smartphone powers off and consumption stops (the value does not become negative).” - If \(A_i - (B_i \times T) > 0\), then that value is the remaining battery as is. - If \(A_i - (B_i \times T) \leq 0\), this means the battery ran out at some point, so the remaining battery is \(0\).
Expressing this mathematically, the remaining battery of smartphone \(i\) at time \(T\) is \(\max(0, A_i - B_i \times T)\).
Algorithm
- Initialize a variable
total_remainingto \(0\) to hold the total remaining battery. - For each of the \(N\) smartphones, repeat the following:
- Compute the calculated remaining battery at time \(T\):
remaining = A_i - B_i * T. - Only if
remainingis greater than \(0\), add that value tototal_remaining.
- Compute the calculated remaining battery at time \(T\):
- Output the final
total_remaining.
Complexity
- Time complexity: \(O(N)\)
- Since we perform one calculation per smartphone, the processing finishes in time proportional to \(N\), including reading the input. Since \(N \leq 2 \times 10^5\), this runs sufficiently fast.
- Space complexity: \(O(N)\)
- Since we use
sys.stdin.read().split()to read and store all input into a list at once, memory usage is proportional to the input size.
- Since we use
Implementation Notes
Handling large values: The total battery value can become very large, but since Python natively supports arbitrary-precision integers, we can compute without worrying about overflow.
Fast input: Since \(N\) can be large, reading all input at once using
sys.stdin.read().split()instead of callinginput()repeatedly reduces execution time.Conditional branching: Make sure not to forget the logic that when
remainingis negative, it should not be added (or equivalently, \(0\) should be added instead).Source Code
import sys
def solve():
# 標準入力から全てのデータを読み込む
input_data = sys.stdin.read().split()
if not input_data:
return
# N: スマートフォンの台数, T: 経過時間
N = int(input_data[0])
T = int(input_data[1])
total_remaining = 0
# 各スマートフォンの初期残量 A と消費量 B を処理
for i in range(N):
A = int(input_data[2 + 2 * i])
B = int(input_data[3 + 2 * i])
# 時刻 T における残量を計算 (負になる場合は 0)
remaining = A - B * T
if remaining > 0:
total_remaining += remaining
# 合計を出力
print(total_remaining)
if __name__ == '__main__':
solve()
This editorial was generated by gemini-3-flash-preview.
posted:
last update: