Official

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

Gemini 3.8 Flash (High)

Overview

This problem asks for the optimal order to carry out all \(N\) cold storage units such that the total power consumption is minimized.

Analysis

Naive Approach

There are \(N!\) possible permutations of the cold storage units. Under the constraint \(N \le 200000\), an exhaustive search that tests all permutations will not finish in time (resulting in TLE). Therefore, we need to find a rule to determine the optimal order efficiently.

Analysis via Adjacent Swaps (Exchange Argument)

To determine the optimal order, let us consider how the power consumption changes when the order of two adjacent cold storage units is swapped.

Let \(A\) (time \(T_A\), power \(P_A\)) and \(B\) (time \(T_B\), power \(P_B\)) be two units carried out consecutively, and let \(t\) be the elapsed time right before starting the work on these two units.

  1. When carrying out \(A\) first, followed immediately by \(B\)

    • Completion time of \(A\): \(t + T_A\)
    • Completion time of \(B\): \(t + T_A + T_B\)
    • Total power consumed by these two units:
      \((t + T_A) \times P_A + (t + T_A + T_B) \times P_B\)
  2. When carrying out \(B\) first, followed immediately by \(A\)

    • Completion time of \(B\): \(t + T_B\)
    • Completion time of \(A\): \(t + T_B + T_A\)
    • Total power consumed by these two units:
      \((t + T_B) \times P_B + (t + T_B + T_A) \times P_A\)

The condition for “processing \(A\) first is better (power consumption is smaller or equal)” is Expression 1 \(\le\) Expression 2. Expanding and rearranging this inequality:

\[ t P_A + T_A P_A + t P_B + T_A P_B + T_B P_B \le t P_B + T_B P_B + t P_A + T_B P_A + T_A P_A \]

Canceling the common terms from both sides (\(t P_A, t P_B, T_A P_A, T_B P_B\)), we obtain a very simple inequality:

\[ T_A P_B \le T_B P_A \]

Dividing both sides by \(P_A P_B\), this can also be expressed as \(\frac{T_A}{P_A} \le \frac{T_B}{P_B}\).
Notice that this condition does not depend on the preceding elapsed time \(t\); it depends solely on the properties of units \(A\) and \(B\) themselves.

Intuitively, prioritizing units that take less time to carry out and consume more power per unit time (i.e., larger \(\frac{P}{T}\), or smaller \(\frac{T}{P}\)) allows us to cut down unnecessary power consumption.

Therefore, sorting all cold storage units in the order that satisfies \(T_A P_B < T_B P_A\) will minimize the overall total power consumption.

Algorithm

  1. Read the input \((T_i, P_i)\) for each cold storage unit.
  2. Sort the cold storage units in ascending order using \(T_A \times P_B < T_B \times P_A\) as the comparison criterion between two units \(A\) and \(B\).
  3. Process the cold storage units in the sorted order:
    • Add \(T_i\) of each unit to a variable current_time representing the elapsed time.
    • Multiply the unit’s completion time current_time by \(P_i\), and add this value to the total power consumption total_power.
  4. Output the final value of total_power.

Complexity

  • Time Complexity: \(O(N \log N)\)
    Sorting the cold storage units takes \(O(N \log N)\) time. The subsequent simulation takes \(O(N)\) time, so the overall bottleneck is sorting, which runs sufficiently fast for the constraint \(N \le 200000\).
  • Space Complexity: \(O(N)\)
    Memory is required to store the data for the \(N\) cold storage units in an array.

Implementation Notes

  • Beware of Integer Overflow:
    In the worst-case scenario where \(N = 2 \times 10^5\), \(T_i = 1000\), and \(P_i = 1000\), the final completion time can reach up to \(2 \times 10^5 \times 1000 = 2 \times 10^8\) minutes. The total power consumption can reach approximately \(2 \times 10^8 \times 1000 \times (2 \times 10^5) / 2 \approx 2 \times 10^{16}\), which far exceeds the maximum value of a 32-bit signed integer (approximately \(2 \times 10^9\)). Make sure to use a 64-bit integer type (long long in C++) for the elapsed time, total power consumption, and comparator calculations.

  • Precision-Safe Comparison:
    Evaluating the sorting condition as \(\frac{T_A}{P_A} < \frac{T_B}{P_B}\) using floating-point division introduces the risk of precision errors. It is safer to compare using the cross-multiplied integer form \(T_A \times P_B < T_B \times P_A\).

    Source Code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Fridge {
    long long t, p;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    if (!(cin >> n)) return 0;

    vector<Fridge> fridges(n);
    for (int i = 0; i < n; ++i) {
        cin >> fridges[i].t >> fridges[i].p;
    }

    sort(fridges.begin(), fridges.end(), [](const Fridge& a, const Fridge& b) {
        return a.t * b.p < b.t * a.p;
    });

    long long current_time = 0;
    long long total_power = 0;

    for (int i = 0; i < n; ++i) {
        current_time += fridges[i].t;
        total_power += current_time * fridges[i].p;
    }

    cout << total_power << "\n";

    return 0;
}

This editorial was generated by gemini-3.8-flash-high.

posted:
last update: