Official

D - 荷物の運搬 / Transporting Cargo Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

This problem asks us to divide the items into at most \(K\) contiguous subarrays without changing their order, minimizing the maximum sum of weights in any subarray.

We can determine whether “the load of each truck can be at most \(X\)” using a greedy approach, and find the minimum achievable \(X\) using binary search.

Analysis

Trying all partitions is infeasible

There are \(N-1\) candidate split points between the items. Trying whether or not to place a partition at each point would result in \(2^{N-1}\) candidates in the worst case, which will exceed the time limit for \(N \leq 10^5\).

Therefore, instead of directly searching for the partition itself, we consider fixing a candidate answer and checking whether that value is achievable.

Fixing the maximum truck capacity

Letting \(X\) be the maximum weight limit per truck, we consider the following decision problem:

Can all items be transported using at most \(K\) trucks such that the load of each truck is at most \(X\)?

This check can be done by scanning the items from left to right, loading as much onto the current truck as possible, and using a new truck only when the limit would be exceeded.

For example, consider \(A=(7,2,5,10,8)\) and \(K=2\):

  • If \(X=18\), they can be divided into \(2\) trucks as \((7,2,5)\) and \((10,8)\), so it is feasible.
  • If \(X=17\), greedy loading requires \(3\) trucks: \((7,2,5)\), \((10)\), and \((8)\), so it is infeasible.

Why the greedy approach works

In the greedy approach, the interval loaded onto the first truck is made as long as possible while keeping the sum of weights at most \(X\). Therefore, no other loading strategy can transport further items on the first truck than the greedy strategy.

Similarly, assume that using \(t-1\) trucks, the greedy approach has transported at least as far as any other strategy. Even if the \(t\)-th truck of another strategy reaches further, the items that the greedy approach still needs to transport form a subsegment of that truck’s load. Since all weights are positive, the sum of this subsegment is also at most \(X\).

Thus, after using \(t\) trucks, the greedy approach can transport items at least as far as any other strategy. In other words, the greedy approach minimizes the number of trucks required, so we can correctly determine feasibility by checking whether this count is at most \(K\).

Monotonicity of the decision problem

If the items can be transported with a capacity of \(X\), they can also be transported using the same partition with any capacity larger than \(X\).

Therefore, as \(X\) increases, the result of the decision problem follows this pattern:

  • Infeasible for smaller values
  • Feasible for values greater than or equal to a certain threshold

Using this monotonicity, we can binary search for the minimum value that becomes feasible.

Algorithm

Let \(S=\sum_{i=1}^{N} A_i\) be the total sum of the item weights, and \(M=\max_i A_i\) be the maximum weight of a single item.

1. Determining the search range

The lower bound of the answer can be determined from the following two conditions:

  • Since the heaviest item must be loaded, the answer must be at least \(M\).
  • Since a total weight of \(S\) is transported using at most \(K\) trucks, the answer must be at least \(\lceil S/K \rceil\).

Therefore, we set the lower bound as:

\(low=\max(M,\lceil S/K \rceil)\)

On the other hand, if the capacity limit is \(S\), all items can be transported on a single truck, so we set:

\(high=S\)

We search such that the answer is always contained within the interval \([low,high]\).

Note that in the code, the following cases are handled beforehand:

  • \(K=1\): The answer is \(S\).
  • \(K=N\): Each item can be placed on a separate truck, so the answer is \(M\).

2. Checking feasibility with the midpoint

While \(low<high\), we test \(mid=\lfloor(low+high)/2\rfloor\) as the capacity limit:

  1. Initialize the number of trucks used as trucks = 1 and the current truck load as current = 0.
  2. Process the items from left to right.
  3. If adding the next item keeps the sum at most \(mid\), load it onto the current truck.
  4. If it exceeds \(mid\), use a new truck and place that item as the first item on it.
  5. If the number of trucks used exceeds \(K\), terminate early as infeasible.

3. Updating the search range

  • If feasible, the answer is at most \(mid\), so set high = mid.
  • If infeasible, the answer is strictly greater than \(mid\), so set low = mid + 1.

Finally, the value when \(low=high\) is the answer.

Complexity

  • Time complexity: \(O(N\log S)\)
  • Space complexity: \(O(N)\)

Here, \(S=\sum_{i=1}^{N} A_i\). A single check takes \(O(N)\) time, and binary search performs the check \(O(\log S)\) times. Since \(S \leq 10^{14}\), the number of checks is at most around \(47\).

\(O(N)\) space is used to store the input array. The additional space used in the checking process is \(O(1)\).

Implementation Notes

  • Items that exceed the limit are placed on the next truck.
    In the code, we first do current += weight and then check if the limit is exceeded. If it is exceeded, we set current = weight, making that item the first one on the new truck.

  • Ensure that each item does not exceed the limit.
    Since the lower bound of the search is at least \(M\), the test value \(mid\) is guaranteed to be at least \(A_i\) for all \(i\). Therefore, an item that cannot fit even on an empty truck will never occur.

  • Perform ceiling division using integer arithmetic.
    \(\lceil S/K \rceil\) can be computed as (total + k - 1) // k.

  • Be mindful of the magnitude of the sum.
    \(S\) can be up to \(10^{14}\). While Python handles this natively, languages with fixed-width integers require \(64\)-bit integers.

    Source Code

import sys


def main():
    data = list(map(int, sys.stdin.buffer.read().split()))
    n, k = data[0], data[1]
    a = data[2:]

    total = sum(a)
    largest = max(a)

    if k == 1:
        print(total)
        return
    if k == n:
        print(largest)
        return

    low = max(largest, (total + k - 1) // k)
    high = total

    while low < high:
        mid = (low + high) // 2
        trucks = 1
        current = 0

        for weight in a:
            current += weight
            if current > mid:
                trucks += 1
                if trucks > k:
                    break
                current = weight

        if trucks <= k:
            high = mid
        else:
            low = mid + 1

    print(low)


if __name__ == "__main__":
    main()

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

posted:
last update: