D - 保冷庫を運び出す順番 / Order of Carrying Out the Coolers 解説 by admin
Gemini 3.8 Flash (High)Overview
In this problem, we want to minimize the total power consumption until all \(N\) cold storage units are powered off by choosing the optimal order to carry them out. We can find the optimal order by sorting the units in ascending order of \(\frac{T_i}{P_i}\) (or in descending order of power consumption efficiency per minute, \(\frac{P_i}{T_i}\)) and processing them greedily.
Analysis
Naive Approach and Its Limitations
If we consider testing every possible ordering (brute-forcing all permutations), there are \(N!\) possible orders. For \(N \le 200000\), this is an astronomical number and will inevitably exceed the time limit (TLE).
Therefore, we need to consider an approach that directly determines the optimal order based on some rule (a greedy algorithm).
Swapping Adjacent Elements (Exchange Argument)
Consider the situation where two units \(A\) (time \(T_A\), power \(P_A\)) and \(B\) (time \(T_B\), power \(P_B\)) are carried out consecutively.
Let \(t\) be the elapsed time before starting work on these two units. Even if we swap the order of \(A\) and \(B\), the total time to finish carrying out both units, \(t + T_A + T_B\), remains the same, so the power consumed by the remaining units is unaffected. Therefore, it is sufficient to compare only the power consumed by \(A\) and \(B\) themselves.
When carried out in the order \(A \to B\):
- Completion time of \(A\): \(t + T_A\) (power consumption: \((t + T_A) \times P_A\))
- Completion time of \(B\): \(t + T_A + T_B\) (power consumption: \((t + T_A + T_B) \times P_B\))
- Total power consumption of the two units: $\(E_{AB} = t(P_A + P_B) + T_A P_A + T_A P_B + T_B P_B\)$
When carried out in the order \(B \to A\):
- Calculating similarly, the total power consumption of the two units is: $\(E_{BA} = t(P_A + P_B) + T_B P_B + T_B P_A + T_A P_A\)$
The condition for the order \(A \to B\) to consume less power is \(E_{AB} < E_{BA}\). Subtracting common terms and simplifying yields: $\(T_A P_B < T_B P_A\)\( Dividing both sides by \)P_A P_B\( (positive real numbers): \)\(\frac{T_A}{P_A} < \frac{T_B}{P_B}\)$
This condition does not depend on the start time \(t\). In other words, no matter the situation, it is always optimal to carry out units with smaller \(\frac{T_i}{P_i}\) first. (Intuitively, this also makes sense: we should prioritize turning off units that take less time \(T_i\) and consume more power \(P_i\) as early as possible.)
Algorithm
- Sort the input pairs of cold storage units \((T_i, P_i)\) in ascending order (from smallest to largest) of the evaluation value \(\frac{T_i}{P_i}\).
- Carry out the units in the sorted order.
- Initialize a variable
curr_trepresenting the current elapsed time to \(0\), and perform the following for each unit:- Add \(T_i\) to
curr_t(updating the completion time). - Add
curr_t * P_ito the total power consumption.
- Add \(T_i\) to
- Output the final total power consumption.
Complexity
- Time Complexity: \(O(N \log N)\)
- Sorting the units takes \(O(N \log N)\).
- The subsequent simulation (accumulating power consumption) takes \(O(N)\).
- Since \(N \le 200000\), this will comfortably run within the time limit.
- Space Complexity: \(O(N)\)
- \(O(N)\) memory is used to store the input data and unit information.
Implementation Notes
Floating-point comparisons: Since the constraints in this problem are small (\(1 \le T_i, P_i \le 1000\)), sorting with Python’s double-precision floating-point division
T / Pas the key provides sufficient precision and will not cause any incorrect comparisons.Fast I/O: Since \(N\) can be as large as \(200000\), using
sys.stdin.read().split()to read all input at once speeds up input processing.Magnitude of the answer: The final answer can reach up to approximately \(\frac{N(N+1)}{2} \times \max(T) \times \max(P) \approx 2 \times 10^{16}\). While this fits within a 64-bit integer, it will overflow a 32-bit integer. Therefore, when implementing in other languages (such as C++), a 64-bit integer type (like
long long) must be used (in Python, arbitrary-precision integers are handled automatically, so there is no need to worry).Source Code
import sys
def main():
input_data = sys.stdin.read().split()
if not input_data:
return
N = int(input_data[0])
vals = list(map(int, input_data[1:]))
items = [(vals[i], vals[i + 1]) for i in range(0, 2 * N, 2)]
items.sort(key=lambda x: x[0] / x[1])
ans = 0
curr_t = 0
for t, p in items:
curr_t += t
ans += curr_t * p
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gemini-3.8-flash-high.