B - ピアノ練習 / Piano Practice Editorial by admin
Claude 4.5 OpusOverview
This problem asks you to find the total performance time when playing \(N\) songs in order, under the condition that if a song has higher difficulty than the previous song, its performance time is halved.
Analysis
Understanding the Problem
First, let’s organize the rules for each song’s performance time: - Song 1: Always takes \(D_1\) minutes - Song 2 onwards: If the difficulty is strictly increasing compared to the previous song (\(D_{i-1} < D_i\)), it takes \(D_i / 2\) minutes (rounded down); otherwise, it takes \(D_i\) minutes
Understanding Through an Example
For example, with \(N = 4\) and difficulties \(D = [3, 5, 4, 8]\): - Song 1 (difficulty 3): No comparison target → 3 minutes - Song 2 (difficulty 5): \(3 < 5\) is true, so halved → 2 minutes (\(5 / 2 = 2\)) - Song 3 (difficulty 4): \(5 < 4\) is false, so normal → 4 minutes - Song 4 (difficulty 8): \(4 < 8\) is true, so halved → 4 minutes (\(8 / 2 = 4\))
Total: \(3 + 2 + 4 + 4 = 13\) minutes
Why a Simple Solution is Sufficient
In this problem, the performance time for each song is determined only by “comparison with the previous song.” There’s no need to look at the entire history, and it can be solved with a single loop, so no special algorithm is required.
Algorithm
- Initialize total time
totalwith the difficulty of the first song \(D_0\) - Iterate through songs 2 to \(N\) in order (\(i = 1, 2, \ldots, N-1\))
- If \(D_{i-1} < D_i\), add \(D_i / 2\) (rounded down) to
total - Otherwise, add \(D_i\) to
total
- If \(D_{i-1} < D_i\), add \(D_i / 2\) (rounded down) to
- Output the final
total
total = D[0]
for i = 1 to N-1:
if D[i-1] < D[i]:
total += D[i] // 2
else:
total += D[i]
Complexity
- Time complexity: \(O(N)\)
- Because we process each song exactly once
- Space complexity: \(O(N)\)
- Because we store the list of difficulties
Implementation Notes
Integer division with floor: In Python, using the
//operator performs integer division that rounds down (truncates the decimal part)Loop starting position: Since the first song is a special case (always \(D_0\) minutes), we initialize with
total = D[0]and start the loop from the second song (index 1)Overflow considerations: \(D_i\) can be up to \(10^9\) and \(N\) can be up to \(2 \times 10^5\), so the total can reach approximately \(2 \times 10^{14}\). However, Python handles arbitrary-precision integers automatically, so there’s no issue (in C++ and similar languages,
long longwould be required)Source Code
N = int(input())
D = list(map(int, input().split()))
total = D[0]
for i in range(1, N):
if D[i-1] < D[i]:
total += D[i] // 2
else:
total += D[i]
print(total)
This editorial was generated by claude4.5opus.
posted:
last update: