公式

B - ピアノ練習 / Piano Practice 解説 by admin

Gemini 3.0 Flash

Overview

This problem asks you to find the total time required to perform \(N\) piano practice pieces in order, given a rule where the performance time is halved if a specific condition is met (the current piece has a higher difficulty than the immediately preceding piece).

Analysis

The most important point in this problem is that “the performance time of a given piece depends only on the difficulty of the immediately preceding piece.”

Specifically, the performance time of the \(i\)-th piece (\(i \ge 2\)) is determined by the following conditions: - When \(D_{i-1} < D_i\): \(D_i / 2\) minutes (rounded down) - When \(D_{i-1} \ge D_i\): \(D_i\) minutes

For the first piece (\(D_1\)), there is no preceding piece to compare against, so it always takes \(D_1\) minutes.

This rule is very simple, and there is no need to consider the state of previous pieces (such as how many consecutive pieces had increasing difficulty up to that point). Therefore, the total time can be calculated by simply traversing the array from the beginning and comparing adjacent elements.

Checking the constraints, the number of pieces \(N\) is at most \(2 \times 10^5\), and the difficulty \(D_i\) is at most \(10^9\). - Since \(N\) is large, an \(O(N^2)\) solution using nested loops would exceed the time limit (TLE), but an \(O(N)\) solution that requires only a single pass is more than sufficient. - The total time can potentially reach around \(10^9 \times 2 \times 10^5 = 2 \times 10^{14}\), so a data type capable of handling large numbers must be used (in Python, this is handled automatically).

Algorithm

  1. Prepare a variable total_time to hold the total time, and initialize it with the difficulty of the first piece \(D_1\).
  2. From the 2nd piece to the \(N\)-th piece, repeat the following process in order:
    • Compare the difficulty \(D_i\) of the current piece with the difficulty \(D_{i-1}\) of the previous piece.
    • If \(D_{i-1} < D_i\), add \(D_i // 2\) (floor division) to total_time.
    • Otherwise, add \(D_i\) as-is to total_time.
  3. Output the final value of total_time.

Complexity

  • Time complexity: \(O(N)\)
    • Since each of the \(N\) elements is examined exactly once in order, the computation finishes in time proportional to the number of pieces \(N\).
  • Space complexity: \(O(N)\)
    • \(O(N)\) memory is used to store the \(N\) input difficulty values in a list.

Implementation Notes

  • Floor division: In Python, the // operator can be used to obtain the result of integer division with the fractional part truncated (as an integer).

  • Index range: When iterating, \(i=0\) (the 1st piece) is used as the initial value, and the range is set so that comparisons are made from \(i=1\) to \(N-1\).

  • Reading large inputs: Using sys.stdin.read().split() allows large amounts of input data to be read efficiently.

    Source Code

import sys

def solve():
    # 入力を標準入力からすべて読み込む
    input_data = sys.stdin.read().split()
    if not input_data:
        return
    
    # 曲の数 N
    n = int(input_data[0])
    # 各曲の難易度 D_1, D_2, ..., D_N
    d = list(map(int, input_data[1:]))
    
    # 最初の曲は比較対象がないため、必ず D_1 分かかる
    total_time = d[0]
    
    # 2番目以降の曲について計算
    for i in range(1, n):
        # 直前の曲より難易度が高い場合、かかる時間は半分(切り捨て)
        if d[i] > d[i-1]:
            total_time += d[i] // 2
        else:
            # そうでない場合はそのままの難易度分の時間がかかる
            total_time += d[i]
            
    # 合計時間を出力
    print(total_time)

if __name__ == '__main__':
    solve()

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

投稿日時:
最終更新: