公式

A - バッテリー残量と充電器の到着 / Battery Level and Charger Arrival 解説 by admin 印刷ページ

Gemini 3.0 Flash

Overview

There are \(N\) smartphones, and the battery decreases by \(D\) percent each day. The problem asks us to find the number of smartphones that have at least \(1\) percent battery remaining after \(K\) days.

Analysis

The most important aspect of this problem is “efficiently calculating the battery level after \(K\) days”.

If we simulate reducing the battery day by day, the number of days \(K = 10^9\) is extremely large, so the computation won’t finish in time and will result in a Time Limit Exceeded (TLE). However, since the battery decreases by \(D\) percent each day, we can rephrase this as the total decrease over \(K\) days is \(D \times K\) percent.

Therefore, for each smartphone \(i\), we just need to determine whether the following condition holds: $\((W_i - D \times K) \geq 1\)$

Rearranging this expression gives: $\(W_i \geq (D \times K) + 1\)$

Here, looking at the constraints in the problem statement, the initial battery level \(W_i\) is at most \(100\) percent. If \(D \times K\) is \(100\) or more, then every smartphone will inevitably reach \(0\) percent or below, so we can immediately determine that the answer is \(0\).

Algorithm

  1. Calculate the total battery loss over \(K\) days: \(loss = D \times K\).
  2. If \(loss \geq 100\), all smartphones’ batteries are depleted, so output 0 and terminate.
  3. If \(loss < 100\), for each smartphone \(W_i\), perform the following check:
    • If \(W_i \geq loss + 1\), increment the count by \(1\).
  4. Output the final count.

Complexity

  • Time complexity: \(O(N)\)
    • Since we check each of the \(N\) smartphones exactly once, the processing completes in time proportional to the input size. It does not depend on the size of \(K\).
  • Space complexity: \(O(N)\)
    • This is the memory required to store the \(N\) battery levels in a list.

Implementation Notes

  • Fast I/O: Since the number of smartphones \(N\) can be as large as \(10^5\), in Python it is faster to read all input at once using sys.stdin.read().split() rather than calling input() repeatedly, reducing execution time.

  • No overflow concerns: Python can handle arbitrarily large integers, so even if \(D \times K\) becomes a very large value, no special handling is needed for the computation.

    Source Code

import sys

def solve():
    # 入力を一度にすべて読み込み、空白で分割してリストにする
    # これにより、大量のデータに対しても高速に処理が可能
    data = sys.stdin.read().split()
    if len(data) < 3:
        return
    
    # スマートフォンの台数 N, 1日あたりの減少量 D, 到着までの日数 K を取得
    n = int(data[0])
    d = int(data[1])
    k = int(data[2])
    
    # K日間に減少するバッテリーの総量
    loss = d * k
    
    # スマートフォンの初期バッテリー残量 W_i は最大で 100 パーセントである。
    # 減少量 loss が 100 以上のとき、どのスマートフォンも K 日後には 0 パーセント以下となる。
    # バッテリー残量が 1 パーセント以上残る必要があるため、この場合は生存数は 0 となる。
    if loss >= 100:
        print(0)
        return
    
    # 生存条件: W_i - loss >= 1  =>  W_i >= loss + 1
    # 判定の基準となる閾値を計算する
    threshold = loss + 1
    
    count = 0
    # 各スマートフォンの現在のバッテリー残量 W_i について判定を行う
    # W_i はリストの 4 番目(インデックス 3)から N 個分並んでいる
    for i in range(3, 3 + n):
        if int(data[i]) >= threshold:
            count += 1
            
    # 結果を出力
    print(count)

if __name__ == '__main__':
    solve()

This editorial was generated by gemini-3-flash-preview.

投稿日時:
最終更新: