B - 相場の値動き / Market Price Movements Editorial by admin
gpt-6-astra-mediumOverview
Compare the price on each day from Day \(2\) to Day \(N\) with the previous day’s price, and count the number of days the price increased, decreased, or stayed flat.
Analysis
Which category day \(i\) falls into depends solely on \(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 naive approach of comparing adjacent prices in order is fast enough. No complex optimization is required.
Although this can be solved by storing all prices in an array, only the previous day’s price is needed for each comparison. Thus, by storing the previous day’s price in a variable prev, you can tally the counts as you read the prices.
For example, when 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 |
Since there is \(1\) increase day, \(1\) decrease day, and \(1\) flat day, the output is 1 1 1.
Note that Day \(1\) has no previous day to compare against, so it is not counted in any category.
Algorithm
- Read \(N\), and store the price on Day \(1\) in
prev. - Initialize the number of increase days
up, decrease daysdown, and flat daysflatall to \(0\). - Repeat the following process from Day \(2\) through Day \(N\):
- Read the current day’s price into
cur. - If
cur > prev, incrementupby \(1\). - If
cur == prev, incrementflatby \(1\). - Otherwise, increment
downby \(1\). - Update
prevtocurin preparation for the next day’s comparison.
- Read the current day’s price into
- Print
up,down, andflatin that order.
Because prev holds the previous day’s price before each day’s comparison, all target days will be correctly classified.
Complexity
- Time Complexity: \(O(N)\)
\(N-1\) comparisons are performed, each taking constant time. - Space Complexity: \(O(1)\)
Without storing an array of prices, only a constant number of variables are used, such as the previous/current prices and count variables.
Implementation Notes
Initialize
prevwith the first input value \(A_1\), not an arbitrary initial value. This prevents Day \(1\) from being mistakenly counted.Execute
prev = curafter finishing the current day’s classification. If updated before the comparison, you will always end up comparing identical prices.The output order is increase days, decrease days, flat days. Be careful, as this may differ from the order of condition checks in your code.
Since every target day is categorized into one of the three types,
up + down + flatwill equal \(N-1\) in the end.Source Code
// 問題文から独立に実装: 前日の値を覚えながら読む
#include <iostream>
int main() {
int n; std::cin >> n;
int prev; std::cin >> prev;
int up = 0, down = 0, flat = 0;
for (int i = 1; i < n; ++i) {
int cur; std::cin >> cur;
if (cur > prev) up++;
else if (cur == prev) flat++;
else down++;
prev = cur;
}
std::cout << up << " " << down << " " << flat << std::endl;
}
This editorial was generated by gpt-6-astra-medium.