C - 気温の均し / Smoothing the Temperature 解説 by admin
Claude 4.6 Opus (Thinking)Overview
This problem asks us to perform one operation that replaces the temperatures of \(K\) consecutive points with their average value, minimizing the maximum temperature across all points after the operation. We efficiently evaluate all operation candidates to find the optimal choice.
Analysis
Structure of the Post-Operation Maximum
When we choose a position \(l\) and apply the operation to the group \([l, l+K-1]\), the maximum value across all points after the operation is the larger of the following two values:
- Value within the group: All points in the group are replaced by the average value \(\frac{F_l + F_{l+1} + \cdots + F_{l+K-1}}{K}\), so the maximum within the group is this average value itself.
- Value outside the group: The temperatures of points not included in the group remain unchanged, so their maximum stays the same.
In other words, the post-operation maximum is: $\(\max\left(\frac{\sum_{i=l}^{l+K-1} F_i}{K},\ \max_{i \notin [l, l+K-1]} F_i\right)\)$
Efficient Computation
We compute the above for all \(l\) (\(N - K + 1\) candidates) and select the minimum.
- Group average: Can be computed in \(O(1)\) by precomputing prefix sums.
- Maximum outside the group: Can be computed in \(O(1)\) by precomputing the prefix max (cumulative maximum from the front) and suffix max (cumulative maximum from the back).
For example, when \(N=5, K=3, F = [3, 1, 4, 1, 5]\): - \(l=0\): average \(= (3+1+4)/3 = 8/3 \approx 2.67\), max outside group \(= \max(1, 5) = 5\), overall max \(= 5\) - \(l=1\): average \(= (1+4+1)/3 = 2\), max outside group \(= \max(3, 5) = 5\), overall max \(= 5\) - \(l=2\): average \(= (4+1+5)/3 = 10/3 \approx 3.33\), max outside group \(= \max(3, 1) = 3\), overall max \(\approx 3.33\)
In this case, \(l=2\) is optimal.
Algorithm
- Build prefix sums: Compute \(\text{prefix}[i] = F_0 + F_1 + \cdots + F_{i-1}\)
- Build prefix max: Compute \(\text{prefix\_max}[i] = \max(F_0, F_1, \ldots, F_{i-1})\)
- Build suffix max: Compute \(\text{suffix\_max}[i] = \max(F_i, F_{i+1}, \ldots, F_{N-1})\)
- Evaluate for each \(l\):
- Group average \(= (\text{prefix}[l+K] - \text{prefix}[l]) / K\)
- Max outside group \(= \max(\text{prefix\_max}[l],\ \text{suffix\_max}[l+K])\)
- Post-operation max \(= \max(\text{group average},\ \text{max outside group})\)
- Output the minimum of the post-operation maximum over all \(l\)
Complexity
- Time complexity: \(O(N)\) (precomputation in \(O(N)\), evaluation of each candidate in \(O(1)\) × \(O(N-K+1)\) candidates)
- Space complexity: \(O(N)\) (arrays for prefix sums, prefix max, and suffix max)
Implementation Notes
Boundaries of prefix_max and suffix_max: Initialize \(\text{prefix\_max}[0] = -\infty\) and \(\text{suffix\_max}[N] = -\infty\). This allows unified handling of the cases where \(l=0\) (no elements outside the group on the left) or \(l=N-K\) (no elements outside the group on the right).
Floating-point output: Since the average calculation involves integer division, it naturally becomes a floating-point number in Python. Output with sufficient precision (e.g., 10 decimal places).
Fast input: Since \(N\) can be as large as \(10^6\), we use bulk reading with
sys.stdin.buffer.read()to speed up input processing.Source Code
import sys
def solve():
input_data = sys.stdin.buffer.read().split()
N = int(input_data[0])
K = int(input_data[1])
F = [int(input_data[i + 2]) for i in range(N)]
# We need to choose l (1-indexed) such that we replace F[l-1..l+K-2] with their average.
# After replacement, the max over all positions should be minimized.
# For a given choice of l (0-indexed: replacing F[l..l+K-1]):
# - The group average is sum(F[l..l+K-1]) / K
# - The max outside the group is max of F[i] for i not in [l, l+K-1]
# - The overall max is max(group_average, max_outside)
# We want to minimize over all valid l of max(avg(l), max_outside(l))
# Precompute prefix sums
prefix = [0] * (N + 1)
for i in range(N):
prefix[i + 1] = prefix[i] + F[i]
# Precompute prefix max and suffix max for computing max outside [l, l+K-1]
# prefix_max[i] = max of F[0..i-1], with prefix_max[0] = -inf
# suffix_max[i] = max of F[i..N-1], with suffix_max[N] = -inf
NEG_INF = float('-inf')
prefix_max = [NEG_INF] * (N + 1)
for i in range(N):
prefix_max[i + 1] = max(prefix_max[i], F[i])
suffix_max = [NEG_INF] * (N + 1)
for i in range(N - 1, -1, -1):
suffix_max[i] = max(suffix_max[i + 1], F[i])
best = float('inf')
for l in range(N - K + 1):
# Group is [l, l+K-1]
group_sum = prefix[l + K] - prefix[l]
avg = group_sum / K
# Max outside the group
max_outside = max(prefix_max[l], suffix_max[l + K])
current_max = max(avg, max_outside)
if current_max < best:
best = current_max
print(f"{best:.10f}")
solve()
This editorial was generated by claude4.6opus-thinking.