D - 保冷庫を運び出す順番 / Order of Carrying Out the Coolers Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
The power consumption of each cold storage unit is given by (completion time of removal) \(\times\) (power consumption per minute). By comparing the order of two adjacent units, we can minimize the total power consumption by moving them in ascending order of \(T_i/P_i\).
Analysis
Expressing Power Consumption in Terms of Completion Time
Let \(C_i\) be the time when the removal of cold storage unit \(i\) is completed.
Since cold storage unit \(i\) consumes power both while waiting and during the removal process, its power consumption is \(P_iC_i\). Therefore, the value we want to minimize is:
\(\displaystyle \sum_{i=1}^{N} P_iC_i\)
For example, consider the case where \((T_1,P_1)=(3,4)\) and \((T_2,P_2)=(2,1)\).
- If we move unit \(1\) then unit \(2\), the completion times are \(3\) and \(5\), respectively, so the total is \(4\times3+1\times5=17\).
- If we move unit \(2\) then unit \(1\), the completion times are \(2\) and \(5\), respectively, so the total is \(1\times2+4\times5=22\).
In this example, moving the unit with the shorter removal time first is not optimal. We need to consider both the removal time and the power consumption rate.
An approach that tries every possible order would require examining \(N!\) permutations, which will not run in time for \(N\leq200000\). Thus, we consider a criterion to determine which unit should be moved first.
Comparing the Order of Two Adjacent Units
Focus on two adjacent cold storage units \(a\) and \(b\) in some order. Let \(S\) be the time when the removal process for these two units begins.
When moving in the order \(a\) → \(b\):
The total power consumption of these two units is:
\(E_{ab}=P_a(S+T_a)+P_b(S+T_a+T_b)\)
When moving in the order \(b\) → \(a\):
Similarly,
\(E_{ba}=P_b(S+T_b)+P_a(S+T_b+T_a)\)
Taking the difference, the common terms cancel out, yielding:
\(E_{ab}-E_{ba}=T_aP_b-T_bP_a\)
Therefore, the condition that moving \(a\) first does not increase the total is:
\(T_aP_b\leq T_bP_a\)
Since \(P_a,P_b\) are positive, this is equivalent to:
\(\displaystyle \frac{T_a}{P_a}\leq\frac{T_b}{P_b}\)
In other words, it is optimal to prioritize cold storage units with a smaller value of removal time divided by power consumption.
Why This Criterion Yields the Global Optimum
Swapping two adjacent units does not change the sum of their removal times. Consequently, the completion times of units moved after them remain unchanged, and units moved before them are not affected either.
Thus, whenever there is an adjacent pair that violates the ascending order of \(T_i/P_i\), swapping those two units can reduce the overall power consumption.
Starting from an arbitrary order and repeatedly performing these swaps, we can sort the units in ascending order of \(T_i/P_i\) without increasing the power consumption. Therefore, this order is optimal. When the ratios are equal, moving either first results in the same total.
Algorithm
- Read \((T_i,P_i)\) for each cold storage unit.
- Sort the units in ascending order of \(T_i/P_i\).
- In actual comparisons, avoid division and determine the order using \(T_aP_b<T_bP_a\).
- Initialize the elapsed time
elapsedand the answeranswerto \(0\). - Process each unit in the sorted order:
- Add the unit’s removal time \(T_i\) to
elapsed. - The value of
elapsedat this point is the completion time for this unit. - Add
elapsed × P_itoanswer.
- Add the unit’s removal time \(T_i\) to
- Output
answer.
Complexity
- Time complexity: \(O(N\log N)\)
Sorting takes \(O(N\log N)\) time, and computing the total power consumption takes \(O(N)\) time. - Space complexity: \(O(N)\)
To store the information for each cold storage unit.
Implementation Details
- Use cross-multiplication to compare ratios
Using integer division for \(T_i/P_i\) loses the fractional part. Furthermore, floating-point types are unnecessary. The code sorts accurately using the following comparison:
return 1LL * x.first * y.second < 1LL * y.first * x.second;
When the ratios are equal, either order is fine, so < is used instead of <= for the comparison.
- Add the removal time before computing the power consumption
A cold storage unit also consumes power while being moved. Therefore, always execute answer += elapsed * p after elapsed += t.
- Use a 64-bit integer for the answer
Completion times can be up to \(2\times10^8\), and the total power consumption exceeds the range of a 32-bit integer. On the other hand, the total is at most \(N\times1000\times(1000N)\leq4\times10^{16}\), so it can be handled using long long.
Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<pair<int, int>> a(N);
for (auto& [t, p] : a) {
cin >> t >> p;
}
sort(a.begin(), a.end(), [](const auto& x, const auto& y) {
return 1LL * x.first * y.second < 1LL * y.first * x.second;
});
long long elapsed = 0;
long long answer = 0;
for (const auto& [t, p] : a) {
elapsed += t;
answer += elapsed * p;
}
cout << answer << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.
posted:
last update: