D - 荷物の運搬 / Transporting Cargo Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
This problem asks us to divide a sequence of packages into at most \(K\) contiguous segments and minimize the maximum sum of weights across all segments. We can solve it by assuming an upper limit on the truck’s capacity and combining greedy checking with binary search.
Analysis
Searching All Partitions Is Infeasible
A partition can be represented by choosing where to place boundaries among the \(N-1\) positions between adjacent packages. However, there are up to \(2^{N-1}\) possible ways to partition, so checking all of them will not run in time.
Therefore, instead of searching for the optimal partition directly, we consider the following decision problem:
Can we transport all packages using at most \(K\) trucks such that the total weight in each truck is at most \(X\)?
Once the Limit Is Fixed, It Can Be Checked Greedily
Let \(X \geq \max_i A_i\). We iterate through the packages from left to right, loading as much onto the current truck as possible, and only use a new truck when adding the next package would exceed \(X\).
For example, consider \(A=(3,1,4,2,5)\) and \(K=3\):
- If \(X=6\), we can transport them using \(3\) trucks: \((3,1)\mid(4,2)\mid(5)\).
- If \(X=5\), we need \(4\) trucks: \((3,1)\mid(4)\mid(2)\mid(5)\).
This greedy approach minimizes the number of trucks required under the capacity limit \(X\).
The reason is that after using the same number of trucks, the greedy approach will have transported at least as far along the sequence of packages as any other valid loading method.
For the first truck, this property is evident because it loads the longest prefix that does not exceed the limit. Subsequently, if the greedy approach has already carried packages up to or beyond the point reached by another method, the remaining packages that the next truck must start loading will be the same or fewer. Since all weights are positive, the next truck in the greedy approach will also reach at least as far as that of the other method.
Therefore, if the greedy approach requires more than \(K\) trucks, no other partitioning method can transport them in \(K\) or fewer trucks either.
Monotonicity of the Feasibility Check
If it is possible to transport the packages with a limit \(X\), it is also possible with any limit greater than \(X\) using the same partition. Conversely, if it cannot be transported with a limit \(X\), it cannot be transported with any limit smaller than \(X\).
In other words, as we increase the limit, the feasibility result transitions exactly once:
“Impossible” \(\to\) “Possible”
We can find the minimum capacity limit at this boundary using binary search.
Algorithm
Let the search range for the answer be \([\mathrm{low},\mathrm{high}]\), initialized as follows:
- \(\mathrm{low}=\max_i A_i\): Since packages cannot be divided, the capacity must be at least the weight of the heaviest package.
- \(\mathrm{high}=\sum_{i=1}^{N}A_i\): All packages can definitely be transported if they are all loaded onto a single truck.
While \(\mathrm{low}<\mathrm{high}\), repeat the following:
- Compute the midpoint \(\mathrm{mid}=\mathrm{low}+\lfloor(\mathrm{high}-\mathrm{low})/2\rfloor\).
- Using \(\mathrm{mid}\) as the capacity limit, count the required number of trucks greedily:
- Initially, set
trucks = 1and the current loadsum = 0. - If adding the next package does not exceed the limit, load it onto the current truck.
- If it exceeds the limit, increment
trucksby \(1\) and load that package onto the new truck.
- Initially, set
- Update the search range based on the result:
- If
trucks <= K, the answer is at most \(\mathrm{mid}\), so sethigh = mid. - Otherwise, the answer is strictly greater than \(\mathrm{mid}\), so set
low = mid + 1.
- If
When \(\mathrm{low}=\mathrm{high}\), output that value.
Since the answer is always contained within \([\mathrm{low},\mathrm{high}]\) throughout the search, the final remaining value is the desired minimum.
Complexity
Let \(S=\sum_{i=1}^{N}A_i\).
- Time Complexity: \(O(N\log S)\)
- Space Complexity: \(O(N)\)
Each feasibility check takes \(O(N)\) time, and binary search performs \(O(\log S)\) checks. Since \(S\leq 10^{14}\), the check is performed at most around \(47\) times.
Implementation Notes
Use
long longfor the sum of weights. The total sum can reach up to \(10^{14}\), which overflows a 32-bit integer.Because
lowis initialized to the maximum package weight, each package is guaranteed to fit individually onto a truck during the check.When
sum + a == mid, that package can still fit onto the current truck. A new truck is only needed whensum + a > mid.As soon as the number of trucks exceeds \(K\), it is guaranteed that the packages cannot be transported with this limit, so the check can be terminated early.
The condition is “at most \(K\) trucks” rather than “exactly \(K\) trucks”, so we check
trucks <= K.Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
cin >> N >> K;
vector<long long> A(N);
long long low = 0, high = 0;
for (auto& a : A) {
cin >> a;
low = max(low, a);
high += a;
}
while (low < high) {
long long mid = low + (high - low) / 2;
int trucks = 1;
long long sum = 0;
for (long long a : A) {
if (sum + a > mid) {
++trucks;
sum = a;
if (trucks > K) break;
} else {
sum += a;
}
}
if (trucks <= K) {
high = mid;
} else {
low = mid + 1;
}
}
cout << low << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.
posted:
last update: