D - 荷物の運搬 / Transporting Cargo Editorial by admin Editorial for printing
Claude Opus 5 (High)Overview
This problem asks us to partition an array into at most \(K\) contiguous subarrays to minimize the maximum sum among these subarrays. This is a classic “minimize the maximum” problem and can be solved by binary searching for the answer.
Analysis
Issues with the Naive Approach
If we try all possible partition points, there are \(2^{N-1}\) ways to choose them, which is far too slow for \(N \leq 10^5\).
One might also consider dynamic programming, defining \(\mathrm{dp}[i][j]\) as “the minimum cost when partitioning the prefix up to the \(i\)-th element into \(j\) subarrays.” However, this requires \(O(NK)\) states and roughly \(O(N^2K)\) time including transitions, which will also result in Time Limit Exceeded (TLE).
Key Insight: Rephrasing as a Decision Problem
Therefore, we rephrase the optimization problem into the following decision problem:
Given a value \(X\), can the items be partitioned into at most \(K\) subarrays such that the load on each truck does not exceed \(X\)?
If we can solve this decision problem, the answer to the original problem is “the minimum \(X\) for which the condition evaluates to true.”
The crucial property here is monotonicity. If a valid partition exists for a certain \(X\), then for any \(X' > X\), the exact same partition remains valid, so it is naturally possible. Conversely, if it is impossible for \(X\), it is also impossible for any value smaller than \(X\). In other words, the decision results follow this pattern:
\[ \underbrace{\text{false}, \text{false}, \dots, \text{false}}_{X\text{ is small}}, \underbrace{\text{true}, \text{true}, \dots}_{X\text{ is large}} \]
Since it has this structure, we can find the boundary using binary search.
Solving the Decision Problem (Greedy Approach)
For a fixed \(X\), the minimum number of trucks required can be determined using a greedy approach.
We simply inspect the items from left to right: “if adding the item to the current truck keeps the total weight at or below \(X\), load it onto the current truck; otherwise, start a new truck.”
Why is this optimal? If we were to intentionally defer an item to the next truck even though it could fit on the current one, it would never increase the number of items we can load later (because starting a subarray earlier can never help us pack more). Thus, it would never decrease the total number of trucks needed. Therefore, packing as much as possible onto each truck is always optimal.
Example: For \(A = (1, 4, 2, 3)\) and \(X = 5\): - 1st truck: \(1 \to 1+4=5\) (OK); next would be \(5+2=7 > 5\), so we stop here. - 2nd truck: \(2 \to 2+3=5\) (OK). - Total: 2 trucks. Thus, if \(K \geq 2\), \(X=5\) is achievable.
Search Range
- Lower bound: \(\max A_i\). In any valid partition, the single largest item must be loaded onto some truck, so the answer cannot be less than \(\max A_i\). (Note: setting the lower bound smaller than this may cause the greedy check to encounter an item that cannot be loaded even on an empty truck, which can cause infinite loops or incorrect results depending on the implementation.)
- Upper bound: \(\sum A_i\). Loading everything onto a single truck is always achievable since \(K \geq 1\).
Algorithm
- Set \(lo = \max A_i\) and \(hi = \sum A_i\).
- While \(lo < hi\), evaluate the decision function \(\mathrm{ok}(mid)\) for \(mid = \lfloor (lo+hi)/2 \rfloor\).
- If \(\mathrm{ok}(mid)\) is true, set \(hi = mid\) (try to see if an even smaller value is possible).
- If false, set \(lo = mid + 1\).
- After the loop terminates, \(lo\) is the answer.
Decision function \(\mathrm{ok}(cap)\):
- Initialize cnt = 1 (number of trucks used) and cur = 0 (current truck load).
- For each \(i\), if cur + A[i] <= cap, update cur += A[i]; otherwise, increment cnt++ and set cur = A[i].
- If cnt > K at any point, return false; if all elements are processed successfully, return true.
Complexity
- Time complexity: \(O(N \log(\sum A_i))\) (Each check takes \(O(N)\), and the binary search performs roughly \(O(\log \sum A_i) \approx 47\) iterations).
- Space complexity: \(O(N)\)
Implementation Notes
Beware of integer overflow: Since \(\sum A_i \leq 10^5 \times 10^9 = 10^{14}\), it will overflow a standard 32-bit integer. In C++, make sure to use
long long, including for the running sum inside the decision function.Lower bound for binary search: As mentioned earlier, it is safest to start from \(\max A_i\). If you start from \(0\) or \(1\), an item with
A[i] > capcannot even fit by itself on an empty truck, which can lead to infinite loops or incorrect checks depending on the implementation (setting the lower bound to \(\max A_i\) guarantees that at least one item will always fit).Calculating
mid: Writingmid = lo + (hi - lo) / 2avoids potential overflow when computing the midpoint.Guarantee of a solution: Since \(K \leq N\), it is always possible to partition the array into at most \(K\) non-empty subarrays. Because the upper bound \(\sum A_i\) is always true, the binary search is guaranteed to converge.
Binary search loop structure: Using the
while (lo < hi)pattern to find the “minimum value that satisfies the condition” (with updateshi = midandlo = mid + 1) ensures that upon termination,lo == hiis the answer, which helps prevent off-by-one errors.Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
int N; long long K;
scanf("%d %lld", &N, &K);
vector<long long> A(N);
long long lo = 0, hi = 0;
for(int i=0;i<N;i++){ scanf("%lld", &A[i]); hi += A[i]; lo = max(lo, A[i]); }
// binary search
auto ok = [&](long long cap)->bool{
long long cnt = 1, cur = 0;
for(int i=0;i<N;i++){
if(cur + A[i] <= cap) cur += A[i];
else { cnt++; cur = A[i]; if(cnt > K) return false; }
}
return cnt <= K;
};
while(lo < hi){
long long mid = lo + (hi - lo)/2;
if(ok(mid)) hi = mid; else lo = mid + 1;
}
printf("%lld\n", lo);
return 0;
}
This editorial was generated by claude5opus-high.
posted:
last update: