B - 相場の値動き / Market Price Movements Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
From day \(2\) to day \(N\), compare each day’s price with the previous day’s price and count the number of days where the price increased, decreased, or remained unchanged (flat).
Analysis
The classification for day \(i\) is determined solely by \(A_i\) and the previous day’s price \(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 inspecting the prices in order from the beginning and comparing adjacent prices is sufficient. Since there are only \(N-1\) comparisons, there is no concern about TLE.
Note that the price is always compared against the “previous day’s price”, not the “first day’s price”. After each comparison, update the stored value of the previous day’s price. Also, the first day is not counted because there is no previous day to compare it against.
For example, if the prices are \([100, 120, 120, 90]\), it proceeds as follows:
| Day | Comparison with previous day | Classification |
|---|---|---|
| Day \(2\) | \(120 > 100\) | Increase day |
| Day \(3\) | \(120 = 120\) | Flat day |
| Day \(4\) | \(90 < 120\) | Decrease day |
Thus, the output is 1 1 1.
Algorithm
- Initialize the count of increase days
up, decrease daysdown, and flat daysflatto \(0\). - Set
prev, representing the previous day’s price, to the first day’s price \(A_1\). - Repeat the following steps from day \(2\) to day \(N\):
- Let the current day’s price be
cur. - If
cur > prev, incrementupby \(1\). - If
cur < prev, incrementdownby \(1\). - Otherwise, increment
flatby \(1\). - In preparation for the next day’s comparison, set
prev = cur.
- Let the current day’s price be
- Output
up,down, andflatin that order.
Each day is inspected exactly once and added to one of the classifications based on its magnitude relation, ensuring that all days are counted correctly.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(N)\)
In the provided code, the entire input is stored using sys.stdin.read().split(), resulting in a space complexity of \(O(N)\). Excluding the input storage, only counters and variables for comparison are used, so the additional space complexity is \(O(1)\).
Implementation Notes
data[0]is the number of days \(N\), anddata[1]is the first day’s price. Therefore,range(2, n + 1)can be used to retrieve the prices from day \(2\) to day \(N\).Convert prices to integers using
intbefore comparing them.Perform
prev = curafter the comparison is complete. If updated before the comparison, you would be comparing identical values.The sum of the counted days will always equal \(N-1\).
The output order is increase days, decrease days, flat days.
Source Code
import sys
def main():
data = sys.stdin.read().split()
n = int(data[0])
up = down = flat = 0
prev = int(data[1])
for i in range(2, n + 1):
cur = int(data[i])
if cur > prev:
up += 1
elif cur < prev:
down += 1
else:
flat += 1
prev = cur
print(up, down, flat)
main()
This editorial was generated by gpt-6-astra-medium.
posted:
last update: