C - 荷物の回収 / Luggage Collection Editorial by admin Editorial for printing
GPT 5.2 HighOverview
Given \(K\) packages to collect (each at a different distance), where you can collect up to \(M\) packages per round trip, minimize the total round-trip distance \(2d\) (where \(d\) is the farthest distance in that trip) summed over all trips.
Analysis
- Since all delivery destinations are “on a straight line in the same direction from the distribution center,” the distance required for one round trip is determined solely by “going to the farthest point in that trip and coming back.”
In other words, if the maximum value of the distance set of packages collected in a trip is \(d\), the travel distance is always \(2d\). - Therefore, what matters is which packages are grouped into the same round trip, as the grouping affects the “maximum distance (= cost).”
Key Insight (Optimal Grouping)
- If the maximum distance in a round trip is \(d\), adding “packages closer than \(d\)” to that trip does not change the maximum distance, so no additional cost is incurred.
- Therefore, it is optimal to prioritize making farther packages the representative (maximum distance) of a group, and collect as many closer packages as possible along the way.
In other words:
- Look at distances in descending order, making the farthest package the “maximum distance” of one round trip
- In that round trip, up to \(M-1\) additional closer packages can be collected together
- Then, among the remaining packages, make the farthest one the maximum distance of the next round trip
…and repeat this process.
Why a Naive Approach is Difficult
“Which packages to assign to which round trip” is a combinatorial optimization problem. Brute-force search or straightforward DP would cause the state space to explode, making it infeasible for \(K \le 2\times 10^5\).
This problem can be solved optimally with the greedy approach above, thanks to the property that “it’s a straight line, and the round-trip cost is determined solely by the maximum distance.”
Concrete Example
For example, with distances in ascending order \([1, 3, 6, 10, 11]\) and \(M=2\): - 11 is the farthest and must be the maximum in some trip. Collect 10 in the same trip (cost is \(2\times 11\)) - The next farthest remaining is 6. Collect 3 along with it (cost is \(2\times 6\)) - Remaining: 1 (cost is \(2\times 1\))
The sum of maximum distances is \(11+6+1\), and the answer is \(2(11+6+1)\).
This is equivalent to “bundling 2 at a time from the largest” = “looking in descending order and adding only the maximum every 2 steps.”
Algorithm
- For delivery destinations with collection requests, gather only the distances \(D_i\) into an array
ds(the destination number \(B_i\) is not needed for cost calculation). - Sort
dsin ascending order. - Starting from the farthest, for every \(M\) packages, add only the first one (the maximum distance of that group) to the total.
Specifically, starting from the last indexi = K-1of the sorted array:total += ds[i]i -= MRepeat untili < 0.
- Since the final answer involves round trips, output \(2 \times total\).
Here, ds[i] represents the “maximum distance” of each round trip, and their sum is minimized.
Complexity
- Time complexity: Dominated by sorting, \(O(K \log K)\)
- Space complexity: \(O(K)\) for storing the distance array
Implementation Notes
The input \(B_i\) (destination number) is unrelated to the ordering of distances and is unnecessary for this optimization, so it can be discarded after reading.
The answer can be at most approximately \(2 \times K \times 10^9\), but Python’s integers do not overflow so there is no concern (in other languages, 64-bit integers would be required).
Source Code
import sys
def main():
input = sys.stdin.readline
N, K, M = map(int, input().split())
ds = []
for _ in range(K):
_, d = map(int, input().split())
ds.append(d)
ds.sort()
total = 0
i = K - 1
while i >= 0:
total += ds[i]
i -= M
print(total * 2)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
posted:
last update: