Official

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

gpt-6-astra-medium

Overview

The power consumption of each cold storage unit is given by “the time until its removal is completed \(\times\) the power consumption per minute”. We can minimize the total power consumption by moving them out in ascending order of the ratio of removal time \(T_i\) to power consumption \(P_i\), i.e., \(T_i/P_i\).

Analysis

Formulating the Power Consumption

Let \(C_i\) be the time when the removal of cold storage unit \(i\) is completed. Since this cold storage unit consumes power from time \(0\) to \(C_i\), its power consumption is \(P_iC_i\).

Therefore, the value we want to minimize is \(\sum_{i=1}^{N}P_iC_i\).

Trying all possible orderings would result in \(N!\) permutations, which will not run in time for \(N \leq 200000\). Furthermore, simply sorting in ascending order of removal time is not necessarily optimal, as it might force units that consume a lot of power to wait for a long time.

For example, when \((T_1,P_1)=(1,1)\) and \((T_2,P_2)=(2,100)\):

  • In the order \(1 \to 2\): \(1 \times 1 + 100 \times 3 = 301\)
  • In the order \(2 \to 1\): \(100 \times 2 + 1 \times 3 = 203\)

Thus, it is sometimes better to move out a unit with a longer removal time first.

Comparing Two Adjacent Units

Consider two adjacent cold storage units \(a\) and \(b\) in some order. Let \(s\) be the time when work on these two units begins.

If we move them out in the order \(a \to b\), the total power consumption of these two units is

\(P_a(s+T_a)+P_b(s+T_a+T_b)\).

On the other hand, in the order \(b \to a\), it is

\(P_b(s+T_b)+P_a(s+T_b+T_a)\).

Subtracting the latter from the former, the difference is \(T_aP_b-T_bP_a\). Therefore, the condition where moving \(a\) first is optimal (or not worse) is

\(T_aP_b \leq T_bP_a\).

Since \(P_a,P_b\) are positive, this is equivalent to

\(\dfrac{T_a}{P_a} \leq \dfrac{T_b}{P_b}\).

Why Sorting by Smaller Ratio Works

Even if we swap these two units, the total time required to move both remains \(T_a+T_b\). Therefore, the completion times of the units processed before and after them do not change.

Consequently, if there are two adjacent units where the order of \(T_i/P_i\) is inverted, swapping them will reduce (or not increase) the total power consumption. Repeating such swaps will eventually sort the sequence in ascending order of \(T_i/P_i\).

Thus, the order that removes units in ascending order of \(T_i/P_i\) is optimal. For units with equal ratios, moving either one first yields the same total.

Algorithm

  1. Store each cold storage unit as a pair \((T_i,P_i)\) in an array.
  2. Sort the array in ascending order of \(T_i/P_i\).
  3. Initialize both the elapsed time elapsed and the total power consumption answer to \(0\).
  4. Process each cold storage unit \((t,p)\) in the sorted order:
    • Add \(t\) to elapsed. This is the completion time of this unit.
    • Add elapsed * p to answer.
  5. Output answer.

After sorting, we simply compute the answer while accumulating the completion times.

Complexity

  • Time Complexity: \(O(N\log N)\)
    • Sorting takes \(O(N\log N)\), and calculating the total takes \(O(N)\).
  • Space Complexity: \(O(N)\)
    • Memory is used for storing the input data and sorting.

Implementation Details

  • Power is also consumed during the removal work.
    Perform elapsed += t first, followed by answer += elapsed * p. If done in reverse, the power consumed during the removal work will not be accounted for.

  • Be careful with the comparison method for sorting.
    The provided code uses job[0] / job[1] as the key. Under the constraints of this problem, the difference between two distinct ratios is at least \(1/1000^2=10^{-6}\), and the value of the ratio is at most \(1000\), so floating-point precision is sufficient to correctly determine the relative order. More generally, comparing \(T_aP_b\) and \(T_bP_a\) using integers avoids precision issues from division.

  • The answer can become a large integer.
    Python handles arbitrarily large integers natively. In other languages, ensure that you use 64-bit integer types for the answer and intermediate products.

    Source Code

import sys


def main():
    data = iter(map(int, sys.stdin.buffer.read().split()))
    n = next(data)
    jobs = [(next(data), next(data)) for _ in range(n)]
    jobs.sort(key=lambda job: job[0] / job[1])

    elapsed = 0
    answer = 0
    for t, p in jobs:
        elapsed += t
        answer += elapsed * p

    print(answer)


if __name__ == "__main__":
    main()

This editorial was generated by gpt-6-astra-medium.

posted:
last update: