C - 温度調整の最小コスト / Minimum Cost of Temperature Adjustment Editorial by admin
Gemini 3.0 FlashOverview
This problem asks for the minimum movement cost to start from 0 degrees, visit all N temperatures, and return to 0 degrees. By thinking of temperatures as points on a number line, this reduces to finding the optimal path.
Analysis
Problem Reformulation
This problem can be thought of as a shortest path problem on a number line: - Starting point: 0 - Points to visit: \(H_1, H_2, \ldots, H_N\) - Ending point: 0
Key Insight
When moving on a number line, repeatedly traversing the same interval back and forth is wasteful.
Let’s consider a concrete example. For \(H = \{-3, 2, 5\}\): - Points to visit are -3, 2, 5 - Minimum is -3, maximum is 5
The optimal strategy is to go from 0 degrees to one end (e.g., 5), then to the opposite end (-3), and finally return to 0 degrees.
0 → 5 → -3 → 0
Cost = 5 + 8 + 3 = 16
With a different order \(0 → -3 → 5 → 0\):
Cost = 3 + 8 + 5 = 16
Both yield the same result!
Why This Is Optimal
- Making one round trip each through the intervals \([\min, 0]\) and \([0, \max]\) is minimal
- Since 0 degrees is both the starting and ending point, we need to go and return in both the positive and negative directions
- Intermediate points are naturally visited along this path
Formula for Minimum Cost
Let \(\max\_positive\) be the maximum positive temperature and \(\min\_negative\) be the minimum negative temperature:
\[\text{Minimum Cost} = 2 \times \max\_positive + 2 \times (-\min\_negative)\]
This is equivalent to \(2 \times (\max\_positive - \min\_negative)\).
Example: For \(H = \{-3, 2, 5\}\) - \(\max\_positive = 5\) - \(\min\_negative = -3\) - Cost \(= 2 \times 5 + 2 \times 3 = 16\)
Algorithm
- Iterate through all \(H_i\)
- Update the maximum positive temperature \(\max\_positive\) (initial value 0)
- Update the minimum negative temperature \(\min\_negative\) (initial value 0)
- The answer is \(2 \times \max\_positive - 2 \times \min\_negative\)
Key Point: By initializing to 0, we correctly handle cases where all temperatures are positive or all are negative.
- If all positive: \(\min\_negative = 0\), resulting in a round trip \(0 → \max → 0\)
- If all negative: \(\max\_positive = 0\), resulting in a round trip \(0 → \min → 0\)
Complexity
- Time Complexity: \(O(N)\) - just one pass through the array
- Space Complexity: \(O(N)\) - storing the input array (can be \(O(1)\) if only computing max/min)
Implementation Notes
- Since the range of \(H_i\) values is \(-10^9 \leq H_i \leq 10^9\), the answer can be up to approximately \(4 \times 10^9\). In Python there’s no concern about integer overflow, but in other languages you need to use
long longtype - By initializing \(\max\_positive\) and \(\min\_negative\) to 0, we naturally handle the starting/ending point of 0 degrees
- Sorting is unnecessary; we only need to find the maximum and minimum values
Source Code
def solve():
N = int(input())
H = list(map(int, input().split()))
# Find minimum energy consumption
# Start from 0 degrees, visit all temperatures, return to 0 degrees
#
# Consider sorting the temperatures
# The optimal path is: go from 0 degrees in one direction, come back and go in the other direction
# Or: go from 0 degrees in only one direction and return
# Find the maximum and minimum of all temperatures
# Need to consider 0 degrees as well (starting and ending point)
if N == 0:
print(0)
return
# Separate into positive and negative temperatures
max_positive = 0 # Maximum positive temperature (0 if none)
min_negative = 0 # Minimum negative temperature (0 if none)
for h in H:
if h > max_positive:
max_positive = h
if h < min_negative:
min_negative = h
# Minimum cost starting from 0 degrees and returning to 0 degrees
# Pattern 1: 0 -> positive max -> negative min -> 0
# Cost = max_positive + (max_positive - min_negative) + (-min_negative) = 2*max_positive - 2*min_negative
# Pattern 2: 0 -> negative min -> positive max -> 0
# Cost = (-min_negative) + (max_positive - min_negative) + max_positive = 2*max_positive - 2*min_negative
#
# Both are the same: 2 * (max_positive - min_negative)
# However, since max_positive >= 0, min_negative <= 0
# Cost = 2 * max_positive + 2 * (-min_negative) = 2 * max_positive - 2 * min_negative
# In practice, making one round trip through the range [min_negative, max_positive] is optimal
# Since 0 degrees is included, starting from 0, go to one end, go to the other end, return to 0
# This allows visiting all temperatures
result = 2 * max_positive - 2 * min_negative
print(result)
solve()
This editorial was generated by claude4.5opus.
posted:
last update: