D - 荷物の運搬 / Transporting Cargo Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
This problem asks us to partition \(N\) items lined up in order into at most \(K\) intervals without changing their order, such that the maximum sum of weights among the intervals is minimized. This is a classic “minimize the maximum” problem, which can be solved efficiently using binary search and a greedy approach.
Analysis
Naive Approach and Its Limitations
If we try all possible ways to partition the items, it corresponds to choosing divider positions among the \(N-1\) spaces between items, resulting in up to \(\binom{N-1}{K-1}\) combinations. While a dynamic programming (DP) approach could be considered, its time complexity would be \(O(N^2 K)\), or around \(O(NK)\) even with prefix sum optimizations. For \(N \leq 10^5\), this will exceed the execution time limit (resulting in TLE).
Reduction to a Decision Problem (Binary Search on Answer)
This problem has the form “minimize the maximum”. For such problems, a standard technique is to shift our perspective and consider the following decision problem:
Decision Problem: “Given that the maximum weight capacity of a single truck is \(P\), can all items be transported using at most \(K\) trucks?”
This decision problem exhibits monotonicity: - As the capacity limit \(P\) increases, each truck can carry more items, so the required number of trucks decreases (or remains the same). - If it is possible to transport all items with capacity \(P\), it is naturally also possible with any larger capacity \(P' (> P)\). - Conversely, if it is impossible with capacity \(P\), it is also impossible with any smaller value.
Therefore, we can efficiently find the “minimum \(P\) satisfying the condition” using binary search.
Solving the Decision Problem (Greedy Approach)
When the capacity limit \(P\) is fixed, a greedy approach is optimal to minimize the number of trucks required. We examine the items from left to right, loading them onto the current truck as long as the total weight does not exceed \(P\). Only when adding the next item would exceed \(P\) do we start loading onto a new truck. If the total number of trucks used in this process is at most \(K\), we determine that it is “feasible”.
Algorithm
- Set the binary search range:
- Lower bound
low: The maximum weight among all items, \(\max(A_i)\) (no matter how many trucks are used, a truck must have at least this capacity to carry the single heaviest item). - Upper bound
high: The sum of the weights of all items, \(\sum A_i\) (the case where a single truck carries all items).
- Lower bound
- Perform binary search:
- Compute the midpoint
mid = low + (high - low) / 2. - Greedily pack the items from front to back with capacity limit
mid, and determine whether the required number of trucks is at most \(K\) (check(mid)). - If feasible with at most \(K\) trucks: Since
midis achievable, record it as a potential answer and sethigh = mid - 1to check if a smaller value is possible. - If not feasible with at most \(K\) trucks: The capacity is insufficient, so set
low = mid + 1.
- Compute the midpoint
- Repeat until
low > high, and output the minimum feasible value found as the answer.
Complexity
- Time Complexity: \(O(N \log(\sum A_i))\)
- Each check in the binary search takes \(O(N)\) time because it scans the items from start to end once.
- The upper bound of the search range is \(\sum A_i \leq 10^5 \times 10^9 = 10^{14}\), meaning the binary search takes approximately \(\log_2(10^{14}) \approx 47\) steps.
- In total, this requires roughly \(47 \times 10^5 \approx 4.7 \times 10^6\) operations, which easily runs well within the time limit.
- Space Complexity: \(O(N)\)
- Requires \(O(N)\) memory to store the array of item weights.
Implementation Details
Handling Integer Overflow: Since the sum of the item weights can reach up to \(10^{14}\), it significantly exceeds the maximum value of a 32-bit integer (
int, approximately \(2 \times 10^9\)). Always use a 64-bit integer type (long longin C++) for capacity calculations and binary search boundaries (such aslow,high,mid, andcurrent_sum).Setting the Binary Search Lower Bound: By setting the initial value of
lowto \(\max(A_i)\), you can implement the greedy check simply, without having to worry about the corner case where a single item exceeds the capacitymidand cannot be loaded onto a truck at all.Source Code
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
long long k;
if (!(cin >> n >> k)) return 0;
vector<long long> a(n);
long long low = 0;
long long high = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
low = max(low, a[i]);
high += a[i];
}
auto check = [&](long long mid) {
long long count = 1;
long long current_sum = 0;
for (int i = 0; i < n; ++i) {
if (current_sum + a[i] > mid) {
count++;
current_sum = a[i];
} else {
current_sum += a[i];
}
}
return count <= k;
};
long long ans = high;
while (low <= high) {
long long mid = low + (high - low) / 2;
if (check(mid)) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
cout << ans << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: