C - 荷物の回収 / Luggage Collection Editorial by admin Editorial for printing
Qwen3-Coder-480BOverview
A problem where you collect packages from \(K\) delivery destinations, minimizing the total travel distance while minimizing the number of round trips considering the truck capacity \(M\).
Analysis
In this problem, the truck can collect at most \(M\) packages in a single round trip, and the travel distance for that round trip is “the farthest distance among the destinations visited in that round trip” × 2. Therefore, to minimize the total travel distance, it is efficient to group together packages from locations that are as close to each other as possible.
Key Observations
- Since the cost of a round trip is “distance to the farthest point × 2,” if you need to go to a far location, it is beneficial to also pick up nearby packages on the same trip.
- Therefore, the optimal strategy is to collect packages in order of distance and divide them into groups of \(M\).
- However, since the distance to the farthest point determines the cost, it is best to form groups that include the farthest point.
Thus, by sorting distances in descending order and taking \(M\) items at a time from the front, we can naturally partition them into groups that minimize the required distance for each round trip.
For example, if the distances are \([1, 3, 5, 7, 9]\) and the truck capacity is \(M=2\): - It is optimal to visit the farthest 9 and next farthest 7 together → travel distance \(2 \times 9 = 18\) - Next, 5 and 3 → travel distance \(2 \times 5 = 10\) - Finally, 1 → travel distance \(2 \times 1 = 2\)
Total distance: \(18 + 10 + 2 = 30\)
In this way, by sorting in descending order and taking \(M\) items at a time from the front, we can always form groups based on the farthest distance.
Algorithm
- Store the distances \(D_i\) of each collection request in a list and sort in descending order.
- Take \(M\) items at a time from the front, and accumulate the maximum distance of each group (i.e., the first element) × 2.
- Repeat this until all packages have been processed.
Complexity
- Time complexity: \(O(K \log K)\) (dominated by sorting)
- Space complexity: \(O(K)\) (array to store distances)
Implementation Notes
Since only the distances are needed, it suffices to extract the list of \(D_i\) from the input and process it.
Sort in descending order, not ascending.
Use the first element of each group (the farthest distance) as the basis for travel distance.
Source Code
import sys
import heapq
def main():
import sys
input = sys.stdin.read
data = input().split()
idx = 0
N = int(data[idx]); idx += 1
K = int(data[idx]); idx += 1
M = int(data[idx]); idx += 1
deliveries = []
for _ in range(K):
b = int(data[idx]); idx += 1
d = int(data[idx]); idx += 1
deliveries.append(d)
# 距離でソート(昇順)
deliveries.sort()
# 最小ヒープを使用して、最大M個の要素を持つグループを作る
# 各グループのコストはその中の最大距離 * 2
# グループ数を最小化することで総距離を最小化する
# 降順で処理して、各往復で最も遠いところからM個取る
deliveries.reverse()
total_distance = 0
i = 0
while i < K:
# 今の往復で回収する荷物の最大距離
max_dist = deliveries[i]
# 次のM個の荷物をこの往復で回収
i += M
total_distance += 2 * max_dist
print(total_distance)
if __name__ == "__main__":
main()
This editorial was generated by qwen3-coder-480b.
posted:
last update: