A - 快適な気温 / Comfortable Temperature Editorial by admin
GPT 5.4 HighOverview
For each day’s forecasted temperature \(T_i\), this is simply a problem of counting the days that satisfy \(L \leq T_i \leq R\).
The answer can be found by going through all \(N\) days in order and counting the number of days that meet the condition.
Analysis
What we want to know in this problem is:
- The day’s temperature is at least the lower bound \(L\)
- The day’s temperature is at most the upper bound \(R\)
How many days satisfy both of these conditions?
In other words, it is sufficient to check once for each day whether it is “a good day for an outing.”
For example, if:
- \(L = 20\)
- \(R = 25\)
- The temperatures are \(18, 20, 23, 27\)
Then:
- \(18\) → out of range
- \(20\) → in range
- \(23\) → in range
- \(27\) → out of range
So the answer is \(2\) days.
Key Observation
The check for each day is independent of the other days.
Therefore, no complex processing is needed. While reading the input, we simply:
- If the condition is satisfied, increment the count by \(1\)
- Otherwise, do nothing
and repeat this process.
Why a Naive Approach Is Sufficient
In this problem, \(N \leq 10^5\), so an \(O(N)\) approach that examines each day exactly once is fast enough.
Conversely, there is no need to:
- Sort the temperatures
- Store everything in an array first and then perform complex processing
Since we are just counting, reading and checking one day at a time is the simplest approach.
Algorithm
- Read \(N, L, R\) as input
- Initialize a counter variable
countto \(0\) - Read the temperatures for all \(N\) days in order
- For each temperature \(t\), if \(L \leq t \leq R\), increment
countby \(1\) - Finally, output
count
In Python, the range check can be written as:
if L <= t <= R:
which makes the implementation very straightforward.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\)
Implementation Notes
The range uses “greater than or equal to” and “less than or equal to,” so boundary values are included.
This means days where \(t = L\) or \(t = R\) must also be counted.Since we check each value on the spot as we read the input, there is no need to store the temperatures in an array.
In the submitted code,
sys.stdin.readlineis used to speed up input reading.Source Code
import sys
def main():
input = sys.stdin.readline
N, L, R = map(int, input().split())
count = 0
for _ in range(N):
t = int(input())
if L <= t <= R:
count += 1
print(count)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.4-high.
posted:
last update: