B - 相場の値動き / Market Price Movements Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
Compare each day’s price with the previous day’s price and count the number of increase days, decrease days, and flat (unchanged) days. The days to be compared are a total of \(N-1\) days, from day \(2\) to day \(N\).
Analysis
The classification of day \(i\) is determined solely by the relationship between \(A_i\) and \(A_{i-1}\):
- If \(A_i > A_{i-1}\), it is an increase day.
- If \(A_i < A_{i-1}\), it is a decrease day.
- If \(A_i = A_{i-1}\), it is a flat day.
Therefore, a straightforward approach of sequentially comparing adjacent prices is well within the time limit. No complex optimizations are needed.
For example, if the prices are \([100, 120, 120, 90]\), the days are classified as follows:
- Day \(2\): \(120 > 100\), so it is an increase day.
- Day \(3\): \(120 = 120\), so it is a flat day.
- Day \(4\): \(90 < 120\), so it is a decrease day.
Thus, the count for each category is \(1\) day.
Also, since each target day is always classified into one of these \(3\) categories, the following equality holds:
\(\text{number of increase days} + \text{number of decrease days} + \text{number of flat days} = N-1\)
In the model solution, the number of increase days and decrease days are counted, and the number of flat days is obtained as the remainder.
Note that for day \(1\), there is no previous day’s record to compare with. Counting day \(1\) or comparing with prices from days other than the immediately preceding day will lead to a wrong answer.
Algorithm
- Read \(N\) and the price array \(A\).
- From day \(2\) to day \(N\), count the days where the price is higher than the previous day, and store the result in
up. - Over the same range, count the days where the price is lower than the previous day, and store the result in
down. - Calculate the number of flat days as
flat = (n - 1) - up - down. - Output
up,down, andflatin this order.
Complexity
- Time complexity: \(O(N)\)
We perform \(N-1\) comparisons each to count the increase days and decrease days. - Space complexity: \(O(N)\)
We store the array of prices. The additional space used besides the array is \(O(1)\).
Implementation Notes
In Python, arrays are \(0\)-indexed. Since
a[0]corresponds to day \(1\), userange(1, n)for comparison and comparea[i]witha[i - 1].sum(1 for ... if condition)counts the number of elements that satisfy the condition by adding \(1\) each time the condition is met.The number of days being compared is \(N-1\), not \(N\). When calculating the number of flat days, subtract from
n - 1.The required output order is increase days, decrease days, flat days. You can print them separated by spaces using
print(up, down, flat).Source Code
# 問題文から独立に実装
n = int(input())
a = list(map(int, input().split()))
up = sum(1 for i in range(1, n) if a[i] > a[i - 1])
down = sum(1 for i in range(1, n) if a[i] < a[i - 1])
flat = (n - 1) - up - down
print(up, down, flat)
This editorial was generated by gpt-6-astra-medium.
posted:
last update: