Official

D - 保冷庫を運び出す順番 / Order of Carrying Out the Coolers Editorial by admin Editorial for printing

imported

Overview

We carry out the cold storage units one by one, aiming to minimize the total electric energy consumed by all units until their removal is complete. By examining the change when swapping two adjacent units, we can sort them using the ratio of removal time to power consumption.

Analysis

If the removal of cold storage unit \(i\) completes at time \(C_i\), the electric energy consumed by this unit is \(P_iC_i\). Therefore, the value to minimize is \(\sum_i P_iC_i\). Checking all possible orderings would require examining \(N!\) permutations. Sorting solely in ascending order of removal time or solely in descending order of power consumption fails to take into account the relationship between both values.

Consider two units \(a\) and \(b\) that are adjacent in some order, and let \(s\) be the time when work on these two units begins. If they are processed in the order \(a, b\), the sum of the electric energy consumed by these two units is

\[ P_a(s+T_a)+P_b(s+T_a+T_b) \]

If we change the order to \(b, a\), the sum becomes

\[ P_b(s+T_b)+P_a(s+T_b+T_a) \]

Subtracting the latter from the former yields

\[ T_aP_b-T_bP_a \]

Since the total time spent on the two units does not change, the completion times of all units other than these two remain unchanged. Therefore, if \(T_aP_b\leq T_bP_a\), processing \(a\) first does not increase the total sum.

Since all \(P_i\) are positive, this condition is equivalent to \(T_a/P_a\leq T_b/P_b\). Thus, we should sort the units in ascending order of \(T_i/P_i\). However, in implementation, we avoid division and compare the cross products \(T_aP_b\) and \(T_bP_a\) using integers. If the ratios are equal, processing them in either order results in the same total.

Correctness

We can transform any arbitrary ordering into an order sorted by non-decreasing ratio. Whenever two adjacent units have inverted ratios, we swap them. From the calculation above, this swap never increases the total electric energy consumed. Since swapping units with equal ratios does not change the total, we may break ties by placing the unit with the smaller index first.

Repeating the operation of eliminating adjacent inversions leads to the units being sorted in ascending order of the ratio, breaking ties in ascending order of index. Since any ordering can be transformed into this order without increasing the total, this ordering is optimal.

By accumulating the removal times in this order, we can obtain the completion time \(C_i\) for each cold storage unit. Summing \(P_iC_i\) gives the actual electric energy consumed for this order, which is the desired minimum value based on the optimality proven above.

Algorithm

  1. For each cold storage unit, store its removal time, electric energy consumption per minute, and its index in the input.
  2. Sort the units using \(T_aP_b<T_bP_a\) to compare two units \(a\) and \(b\). If the cross products are equal, compare by their indices.
  3. Initialize the elapsed time and the answer to \(0\). In sorted order, add each unit’s removal time to the elapsed time, and add the product of the current elapsed time and that unit’s power consumption to the answer.
  4. Output the answer.

Complexity

Let \(N\) be the number of cold storage units.

The time complexity is \(O(N\log N)\). Sorting with constant-time cross-product comparisons takes \(O(N\log N)\), and the subsequent accumulation and summation take \(O(N)\).

The space complexity is \(O(N)\). We store the time, power consumption, and index of each cold storage unit in an array. Auxiliary space for sorting also falls within this bound.

Implementation Notes

The elapsed time is at most \(1000N\). The total electric energy consumed in any order satisfies

\[ 1000^2\frac{N(N+1)}2\leq 20000100000000000 \]

which fits within a signed 64-bit integer. Use long long for the accumulated time and the answer. Furthermore, because comparisons are performed using integer products, there are no floating-point precision issues.

Source Code

#include <bits/stdc++.h>
using namespace std;

struct Box {
    long long time, power;
    int id;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<Box> boxes(n);
    for (int i = 0; i < n; ++i) {
        cin >> boxes[i].time >> boxes[i].power;
        boxes[i].id = i;
    }
    sort(boxes.begin(), boxes.end(), [](const Box &a, const Box &b) {
        long long left = a.time * b.power;
        long long right = b.time * a.power;
        if (left != right) return left < right;
        return a.id < b.id;
    });
    long long elapsed = 0, answer = 0;
    for (const auto &box : boxes) {
        elapsed += box.time;
        answer += elapsed * box.power;
    }
    cout << answer << '\n';
}

This editorial was generated by imported.

posted:
last update: