公式

A - 合格者への拍手 / Applause for the Successful Candidates 解説 by admin

Gemini 3.0 Flash

Overview

Given \(N\) participants, the problem asks you to identify those whose scores are at or above a threshold \(K\), and compute the sum of their scores.

Analysis

This problem can be solved by directly simulating the given conditions.

Specifically, perform the following steps for all participants: 1. Check whether the participant’s score \(P_i\) is at least \(K\). 2. If it is at least \(K\), add that score \(P_i\) to the running total. 3. If it is less than \(K\), do nothing.

The number of participants \(N\) is at most \(10^5\), but the operations performed for each participant are very simple: just a “comparison” and an “addition.” With a time complexity of \(O(N)\), modern computers can perform around \(10^8\) operations per second, so this can be solved well within the time limit.

Algorithm

  1. Initialize a variable total_claps to \(0\) to keep track of the total number of claps.
  2. Take the \(N\) input scores \(P_1, P_2, \ldots, P_N\) one by one in order, and repeat the following process:
    • Let the current score be \(score\). If \(score \ge K\), add \(score\) to total_claps.
  3. After checking all scores, output the final value of total_claps.

Complexity

  • Time complexity: \(O(N)\)
    • We loop through each of the \(N\) participants once to make the decision, so the execution time is proportional to \(N\).
  • Space complexity: \(O(N)\)
    • In the provided code, sys.stdin.read().split() is used to read all input values and store them as a list in memory, consuming memory proportional to the number of inputs \(N\).

Implementation Notes

  • Efficient input: Since \(N\) can be as large as \(10^5\), reading all input at once with sys.stdin.read().split() is faster than calling Python’s standard input() repeatedly, reducing execution time.

  • Condition check: It is important to correctly write the condition “at least \(K\)” from the problem statement as score >= k.

  • Overflow: Python’s int type automatically handles arbitrary-precision integers, so there is no need to worry about overflow even if the total becomes large (though in this problem the maximum value is around \(100 \times 10^5 = 10^7\)).

    Source Code

import sys

def solve():
    # 標準入力から全てのデータを取得
    input_data = sys.stdin.read().split()
    if not input_data:
        return
    
    # N, K を取得
    n = int(input_data[0])
    k = int(input_data[1])
    
    # スコアのリストを取得
    scores = map(int, input_data[2:])
    
    total_claps = 0
    for score in scores:
        # スコアが K 以上の場合、そのスコア分だけ拍手回数に加算
        if score >= k:
            total_claps += score
            
    # 結果を出力
    print(total_claps)

if __name__ == "__main__":
    solve()

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

投稿日時:
最終更新: