A - 登山道の気温変化 / Temperature Changes on a Mountain Trail Editorial by admin
gpt-5.3-codexOverview
This problem requires looking at the temperature differences between adjacent checkpoints in order and counting the number of times \(|T_i - T_{i-1}| \ge K\) is satisfied.
It can be solved by simply comparing each consecutive pair from the beginning.
Analysis
The key point is that the determination of a sudden change depends only on “two adjacent points.”
In other words, to evaluate checkpoint \(i\), only \(T_i\) and \(T_{i-1}\) are needed — no earlier information is required.
For example, when
\(N=5,\ K=4,\ T=[10, 13, 8, 15, 14]\):
- \(|13-10|=3\) (not a sudden change)
- \(|8-13|=5\) (sudden change)
- \(|15-8|=7\) (sudden change)
- \(|14-15|=1\) (not a sudden change)
So the answer is \(2\).
Even with a straightforward approach, nothing beyond “checking all adjacent pairs” is necessary.
This method requires \(N-1\) comparisons, which is sufficiently fast even for the constraint \(N \le 10^5\).
Also, you don’t need to store the entire temperature array — you can make the determination by keeping only the previous temperature prev.
This reduces memory usage.
Algorithm
- Read \(N, K\).
- Store the first temperature \(T_1\) in
prev. - Repeat for \(i=2\) to \(N\):
- Read the current temperature as
cur. - If \(|cur - prev| \ge K\), increment the count by 1.
- Update
prev = cur.
- Read the current temperature as
- Output the count.
This flow matches the provided code.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\) (when not storing the input array)
Implementation Notes
The loop runs from checkpoint \(2\) to \(N\), so use
for _ in range(2, N + 1):.The condition uses “greater than or equal to,” so use
>= K(not>).In Python,
abs(cur - prev)safely computes the absolute value.Since the input can be relatively large, using
sys.stdin.readlineensures stable and fast reading.Source Code
import sys
def main():
input = sys.stdin.readline
N, K = map(int, input().split())
prev = int(input().strip())
count = 0
for _ in range(2, N + 1):
cur = int(input().strip())
if abs(cur - prev) >= K:
count += 1
prev = cur
print(count)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.3-codex.
posted:
last update: