E - 気温変動の監視 / Monitoring Temperature Fluctuations 解説 by admin
claude4.8opus-highOverview
For each observation site, this problem asks us to find the maximum value of the “difference between the maximum and minimum values” over \(K\) consecutive days, and count the number of sites where this value is at least the threshold \(T\).
Analysis
Naive Approach
For each observation site, a naive approach would be to calculate the maximum and minimum values for every consecutive \(K\)-day interval (there are \(M - K + 1\) intervals in total) from scratch.
However, since finding the maximum and minimum in a single interval takes \(O(K)\) time, this would require \(O(M \times K)\) per site and \(O(N \times M \times K)\) overall. With the constraints where \(M\) and \(K\) can be up to \(10^5\) and \(N \times M \leq 2 \times 10^6\), this will not pass within the time limit.
Key to Optimization: Sliding Window Maximum and Minimum
The key is to efficiently update the maximum and minimum values in the interval as we slide a window of width \(K\) one step at a time.
By using the “sliding window maximum (minimum)” technique, we can find the maximum and minimum of each interval in \(O(M)\) time overall. This allows us to process each site in \(O(M)\) time.
Early Termination
Furthermore, once the difference in any interval becomes at least \(T\), we can immediately determine that the site “satisfies the condition,” so we do not need to check the remaining intervals. Breaking out of the loop allows us to skip redundant calculations (this does not change the worst-case time complexity, but makes it faster in practice).
Algorithm
The sliding window maximum and minimum can be implemented using a monotonic deque (double-ended queue).
Let’s describe how the deque maxd for finding the maximum value operates (it stores the indices):
- When adding a new element \(v = S_{i,j}\), we remove any elements from the back of the deque that are less than or equal to \(v\), because they can never become the maximum in the future (since \(v\) is newer and larger).
- Then, we add \(j\) to the back. This maintains the deque in a monotonically decreasing order of values, so the front of the deque always points to the maximum value in the current window.
- If the index at the front of the deque has fallen out of the window’s range (the most recent \(K\) elements), we remove it from the front.
The deque mind for finding the minimum value operates similarly, except we remove elements that are greater than or equal to \(v\) to maintain a monotonically increasing order.
For each day \(j\), once \(j \geq K-1\) (meaning the window has accumulated exactly \(K\) days of data),
\[ S_{i,\text{front of maxd}} - S_{i,\text{front of mind}} \]
gives the difference between the maximum and minimum values in that interval. If this difference is at least \(T\), we increment our count for this site and break out of the loop.
Concrete Example
For example, when the temperatures are [3, 1, 4, 1, 5] and \(K=3\):
- Interval
[3,1,4]\(\rightarrow\) Max 4, Min 1, Difference 3 - Interval
[1,4,1]\(\rightarrow\) Max 4, Min 1, Difference 3 - Interval
[4,1,5]\(\rightarrow\) Max 5, Min 1, Difference 4
The maximum difference is 4. If \(T \leq 4\), this site is counted. Using deques, we can update the maximum and minimum of each interval in \(O(1)\) time per step as we slide the window.
Complexity
For each observation site, each index is added to the deque once and removed at most once. Thus, we can process each site in \(O(M)\) time.
- Time Complexity: \(O(N \times M)\) (Since \(N \times M \leq 2 \times 10^6\) from the constraints, this is fast enough)
- Space Complexity: \(O(M)\) (To store the data and deques for one site)
Implementation Points
Store indices in the deque: By storing indices instead of the actual values, we can determine whether an element has fallen out of the current window. Any index less than or equal to
lim = j - Kis out of the window and should be removed from the front of the deque.Fast I/O: Since \(N \times M\) can be as large as \(2 \times 10^6\), reading the entire input at once using
sys.stdin.buffer.read().split()is very fast. Reading elements one by one usinginput()might result in a Time Limit Exceeded (TLE).Early Termination: By
breaking out of the loop as soon as the condition is satisfied, we can avoid unnecessary computations.Integer comparison is sufficient: Since the temperatures are integers, their differences are also integers, so there is no need to worry about precision errors.
Source Code
import sys
from collections import deque
def main():
data = sys.stdin.buffer.read().split()
idx = 0
N = int(data[idx]); idx += 1
M = int(data[idx]); idx += 1
K = int(data[idx]); idx += 1
T = int(data[idx]); idx += 1
count = 0
for _ in range(N):
row = data[idx:idx+M]
idx += M
arr = [int(x) for x in row]
maxd = deque() # decreasing
mind = deque() # increasing
found = False
for j in range(M):
v = arr[j]
while maxd and arr[maxd[-1]] <= v:
maxd.pop()
maxd.append(j)
while mind and arr[mind[-1]] >= v:
mind.pop()
mind.append(j)
lim = j - K
if maxd[0] <= lim:
maxd.popleft()
if mind[0] <= lim:
mind.popleft()
if j >= K-1:
if arr[maxd[0]] - arr[mind[0]] >= T:
found = True
break
if found:
count += 1
print(count)
main()
This editorial was generated by claude4.8opus-high.
投稿日時:
最終更新: