D - 保冷庫を運び出す順番 / Order of Carrying Out the Coolers 解説 by admin 印刷ページ
claude5.5opus-highOverview
Each refrigerator consumes power equal to “completion time \(\times\) power consumption per minute”. The problem asks us to find the removal order that minimizes the total power consumption. Using an exchange argument—swapping two adjacent refrigerators and comparing the results—we find that sorting them in ascending order of \(T_i / P_i\) is optimal.
Analysis
Power Consumption Formula
Let \(C_i\) be the time when the work for refrigerator \(i\) finishes. Since refrigerator \(i\) continues to consume power from time \(0\) until \(C_i\), its power consumption is \(C_i \times P_i\). Therefore, the value we want to minimize is:
\[\sum_{i} C_i \cdot P_i\]
Naive Approach
There are \(N!\) possible orders to try. Since \(N \le 200000\), this will definitely not finish in time.
Key Insight: Swapping Adjacent Refrigerators
Suppose refrigerator \(i\) and refrigerator \(j\) are adjacent in some order. If the work immediately preceding them finishes at time \(s\), the combined power consumption of these two refrigerators is:
- Order \(i \to j\): \(P_i(s+T_i) + P_j(s+T_i+T_j)\)
- Order \(j \to i\): \(P_j(s+T_j) + P_i(s+T_j+T_i)\)
The completion times of all other refrigerators are unaffected by this swap. Therefore, it is sufficient to compare just these two expressions. Subtracting them cancels out the terms containing \(s\):
\[(i \to j) - (j \to i) = P_j T_i - P_i T_j\]
Thus, the condition under which processing \(i\) before \(j\) is better (or equal) is:
\[T_i P_j < T_j P_i \iff \frac{T_i}{P_i} < \frac{T_j}{P_j}\]
In other words, whenever a refrigerator with a larger \(T/P\) appears before one with a smaller \(T/P\), swapping them will either decrease the total or keep it unchanged. Repeating these swaps ultimately leads to the refrigerators being sorted in ascending order of \(T_i/P_i\). Therefore, this order is optimal. For pairs where \(T/P\) is equal, swapping them does not change the total value, so either order is acceptable.
Intuitively, this means: “The faster a refrigerator can be cleared out and the more power it consumes, the earlier it should be handled.”
Concrete Example
Let us check with two refrigerators: \((T,P) = (3,1), (1,2)\).
- Order 1 → 2: \(3 \times 1 + 4 \times 2 = 11\)
- Order 2 → 1: \(1 \times 2 + 4 \times 1 = 6\)
\(T/P\) is \(3\) for refrigerator 1 and \(0.5\) for refrigerator 2. Processing refrigerator 2—which has the smaller ratio—first gives a smaller total, matching our analysis.
Algorithm
- Sort all refrigerators in ascending order of \(T_i / P_i\).
- Iterating from the beginning, add \(T_i\) to the current time \(t\) to determine the completion time.
- Multiply the completion time by \(P_i\) and add it to the total.
- Output the total.
Complexity
- Time complexity: \(O(N \log N)\) (dominated by sorting)
- Space complexity: \(O(N)\)
Implementation Notes
Floating-point comparisons: We use the floating-point value
T/Pas the sort key. Since \(T, P \le 1000\), the difference between two distinct ratios \(a/b\) and \(c/d\) is at least \(\frac{1}{bd} \ge 10^{-6}\). A difference of this scale can be reliably distinguished usingdoubleprecision without any precision errors. If you prefer to be strict, you can usefunctools.cmp_to_keyto compare \(T_i P_j\) and \(T_j P_i\). However, sorting with a custom comparison function is slower in Python.Size of the answer: The total work time can be up to \(2 \times 10^8\). The answer can reach approximately \(2\times10^8 \times 1000 \times 2\times10^5 = 4 \times 10^{16}\) at most. Python handles arbitrary-precision integers automatically, so this is not an issue, but languages such as C++ require 64-bit integers (
long long).Fast I/O: Because \(N\) is large, reading all input at once using
sys.stdin.buffer.read()will be faster.Source Code
import sys
def main():
data = sys.stdin.buffer.read().split()
n = int(data[0])
T = data[1:2*n+1:2]
P = data[2:2*n+2:2]
items = [(int(T[i]), int(P[i])) for i in range(n)]
items.sort(key=lambda x: x[0] / x[1])
t = 0
total = 0
for a, b in items:
t += a
total += t * b
print(total)
main()
This editorial was generated by claude5.5opus-high.
投稿日時:
最終更新: