D - 保冷庫を運び出す順番 / Order of Carrying Out the Coolers Editorial by admin Editorial for printing
claude5.5opus-highOverview
Let \(C_i\) be the completion time of carrying out cool box \(i\). Then, the total power consumption is \(\sum P_i C_i\). We want to find an order of moving the boxes that minimizes this value.
Analysis
Power Consumption Formula
Cool box \(i\) consumes \(P_i\) units of power per minute from time \(0\) until its completion time \(C_i\). Therefore, the power consumed by cool box \(i\) is \(P_i \cdot C_i\). The total is given by the following formula:
\[\sum_{i} P_i \cdot C_i\]
Issues with Naive Approaches
- Trying all permutations: There are \(N!\) permutations, which will not run in time for \(N = 200000\).
- Sorting by \(T_i\) in ascending order: This yields WA. For example, consider two boxes with \((T,P) = (1,1), (2,100)\).
- Carrying them out in ascending order of \(T\) gives \(1\cdot1 + 3\cdot100 = 301\).
- Carrying them out in reverse order gives \(2\cdot100 + 3\cdot1 = 203\), which is smaller.
- Sorting by \(P_i\) in descending order: Counterexamples can be constructed similarly.
A criterion that takes both \(T\) and \(P\) into account is needed.
Analysis via Adjacent Swaps (Key Insight)
Consider two adjacent boxes \(i\) and \(j\) in some ordering. Let \(s\) be the start time of the operation for \(i\). Swapping the order of these two boxes does not affect the completion times of any other cool boxes. Thus, we only need to compare the power consumption of these two boxes.
- 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)\)
Taking the difference yields:
\[(i\to j) - (j\to i) = P_j T_i - P_i T_j\]
Therefore, the condition under which processing \(i\) first is better is:
\[T_i P_j < T_j P_i \iff \frac{T_i}{P_i} < \frac{T_j}{P_j}\]
In other words, it is optimal to carry them out in ascending order of \(\dfrac{T_i}{P_i}\) (operation time per unit of power). If there is an adjacent pair violating this criterion, swapping them will not increase the total (it will decrease or stay equal). This ordering is known as “Smith’s rule” in scheduling problems.
Intuitively, this means we should prioritize items that “finish quickly” or “consume a lot of power”.
Concrete Example
Consider the case \((T,P) = (3,1), (1,2)\). The ratios are \(3\) and \(0.5\) respectively, so we carry out the second box first.
- Carrying out the 2nd box first: \(1\cdot2 + 4\cdot1 = 6\)
- Carrying out the 1st box first: \(3\cdot1 + 4\cdot2 = 11\)
Indeed, carrying out the second box with the smaller ratio first results in a smaller total.
Algorithm
- Sort all cool boxes in ascending order of \(\dfrac{T_i}{P_i}\). Comparisons are done via \(T_i P_j < T_j P_i\).
- In the sorted order, add \(T_i\) to the current time \(t\). For each cool box, add \(t \cdot P_i\) to the answer.
- Output the resulting sum.
Complexity
- Time complexity: \(O(N \log N)\) (dominated by sorting)
- Space complexity: \(O(N)\)
Implementation Notes
Compare using multiplication: Computing \(T_i/P_i\) using floating-point numbers risks precision issues. Therefore, compare using integer multiplication as \(T_i P_j < T_j P_i\). Since the values are at most \(10^6\), this is safe. Since \(P_i \ge 1\), the direction of the inequality does not change, and the comparison function forms a valid strict weak ordering.
Beware of overflow: The completion time can reach up to \(2\times10^5 \times 1000 = 2\times10^8\). The answer can be at most roughly \(2\times10^8 \times 1000 \times 2\times10^5 = 4\times10^{16}\), which does not fit in a 32-bit integer. It is necessary to use
long long.Since the input is large (up to 200,000 lines), it is advisable to use fast I/O such as
scanf.Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
if(scanf("%d",&n)!=1) return 0;
vector<pair<long long,long long>> a(n);
for(auto&x:a) scanf("%lld %lld",&x.first,&x.second);
sort(a.begin(),a.end(),[](const pair<long long,long long>&x,const pair<long long,long long>&y){
return x.first*y.second < y.first*x.second;
});
long long t=0,ans=0;
for(auto&x:a){
t+=x.first;
ans+=t*x.second;
}
printf("%lld\n",ans);
}
This editorial was generated by claude5.5opus-high.
posted:
last update: