Official

C - 荷物の回収 / Luggage Collection Editorial by admin Editorial for printing

Claude 4.6 Opus (Thinking)

Overview

This problem asks us to minimize the total travel distance when collecting all packages from \(K\) delivery destinations located along a single road from a distribution center, using a truck (with a maximum capacity of \(M\) items) over multiple round trips. An optimal solution is obtained by a greedy approach that groups destinations starting from the farthest ones.

Analysis

Key Insight: The cost of a single round trip is determined by the “farthest destination”

Since the road is a single straight path, when visiting multiple destinations in one round trip, the travel distance is simply twice the distance \(d\) of the farthest destination visited in that trip (i.e., \(2d\) for the round trip). Destinations along the way can be visited at no additional cost.

It’s beneficial to collect closer destinations “along the way” to farther ones

For example, consider 5 destinations at distances \(10, 8, 5, 3, 1\) with \(M = 2\).

  • When going to distance \(10\), also collect the package at distance \(8\) along the way (cost \(2 \times 10 = 20\))
  • When going to distance \(5\), also collect the package at distance \(3\) along the way (cost \(2 \times 5 = 10\))
  • Distance \(1\) is collected on its own (cost \(2 \times 1 = 2\))
  • Total: \(20 + 10 + 2 = 32\)

Conversely, if we paired distance \(10\) with distance \(1\), then distance \(8\) would become the farthest in another trip, increasing the total cost.

Conclusion: Sort by distance in descending order and group into batches of \(M\)

When sorted in descending order of distance, grouping every \(M\) consecutive destinations means that only the farthest destination in each group (i.e., the first element of each group) contributes to the cost. This way, closer destinations can “ride for free” on the cost of farther destinations, minimizing the total travel distance.

Algorithm

  1. Sort the \(K\) distances \(D_1, D_2, \ldots, D_K\) in descending order.
  2. Divide the sorted array into groups of \(M\) consecutive elements from the beginning.
  3. For each group, let \(d\) be the first element (i.e., the maximum distance in that group), and add \(2d\) to the total cost.
  4. The sum of costs across all groups is the answer.

Specifically, after sorting in descending order, the elements at indices \(0, M, 2M, 3M, \ldots\) are the farthest distances for each group, so we simply compute twice the sum of these elements.

The number of round trips is \(\lceil K / M \rceil\).

Complexity

  • Time complexity: \(O(K \log K)\) (dominated by sorting)
  • Space complexity: \(O(K)\) (to store the array of distances)

Implementation Notes

  • The destination numbers \(B_i\) are not needed for solving the problem; only the distances \(D_i\) are used.

  • After sorting in descending order, looping with for i in range(0, K, M) (stepping by \(M\)) allows us to concisely extract the farthest distance of each group.

  • Since \(D_i\) can be up to \(10^9\) and \(K\) can be up to \(2 \times 10^5\), the answer can be approximately \(2 \times 10^{14}\). In Python, there is no concern about integer overflow, but in C++ and similar languages, long long must be used.

    Source Code

import math

def main():
    import sys
    input_data = sys.stdin.read().split()
    idx = 0
    N = int(input_data[idx]); idx += 1
    K = int(input_data[idx]); idx += 1
    M = int(input_data[idx]); idx += 1
    
    distances = []
    for i in range(K):
        B = int(input_data[idx]); idx += 1
        D = int(input_data[idx]); idx += 1
        distances.append(D)
    
    # Sort distances in descending order
    distances.sort(reverse=True)
    
    # Greedy: pick the farthest unvisited location, and take M-1 more with it
    # Each trip covers the farthest location in that group, costing 2 * that distance
    # So we pick every M-th location starting from index 0 (0-indexed) in descending order
    
    total = 0
    for i in range(0, K, M):
        total += 2 * distances[i]
    
    print(total)

main()

This editorial was generated by claude4.6opus-thinking.

posted:
last update: