C - 荷物の搬入 / Loading the Luggage Editorial by admin Editorial for printing
claude4.8opus-highSummary
This problem asks for the minimum cost to transport \(N\) packages from the warehouse (distance \(0\)) to the rooftop (distance \(L\)). In conclusion, the answer is always \(N \times L\).
Analysis
At first glance, the capacity \(K\) (the number of packages that can be carried at once) and the locations of the landings \(D_i\) along the way seem to affect the answer. However, if we carefully observe the definition of the cost, we can see that they have no effect at all.
Understanding the Essence of the Cost
The cost is defined as “the number of packages being carried per unit of movement distance”. Let’s break this down per package:
- While carrying a single package, that package incurs a “cost of \(1\) per unit of distance moved”.
- In other words, the total cost is equal to the sum of the distances each package is carried.
\[\text{Total Cost} = \sum_{\text{package}} (\text{total distance that package is carried})\]
Minimum Distance Each Package Must Be Carried
Each package must ultimately be moved forward from the warehouse (distance \(0\)) to the rooftop (distance \(L\)), which requires moving forward by at least a distance of \(L\) in total. Therefore, the minimum distance each package must be carried is \(L\).
If we carry each package straight forward without any unnecessary backtracking, we can finish transporting them by carrying them for exactly a distance of \(L\). Thus, the minimum cost for each package is \(L\), making the total cost for \(N\) packages \(N \times L\).
Why \(K\) and the Landings Do Not Matter
- The capacity \(K\): Whether we carry multiple packages together or separately, the “distance traveled per package” does not change. Even if we carry them together, the sum of the costs remains the same. Furthermore, moving empty-handed costs \(0\), so making multiple round trips does not incur any extra cost.
- The landings \(D_i\): Even if we temporarily place a package on a landing, that package still needs to be moved forward by a total distance of \(L\) eventually, so the cost does not decrease (in fact, making extra round trips would only increase it).
Therefore, this information consists of “dummy” inputs that do not affect the answer.
Algorithm
Based on the above analysis, the answer is simply calculated as \(N \times L\). We only need to read and ignore the landing distances \(D_i\), and \(K\) is not used at all.
Complexity
- Time Complexity: \(O(M)\) (only for reading and ignoring the input; the calculation itself is \(O(1)\))
- Space Complexity: \(O(1)\)
Implementation Points
Since \(N \leq 10^9\) and \(L \leq 10^9\), \(N \times L\) can be up to approximately \(10^{18}\). This will overflow a 32-bit integer (
int), so we must use a 64-bit integer (long longin C++).Note that when \(M = 0\), there is no input on the second line. However, since the loop count simply becomes \(0\), no special branching is required.
The landing inputs are only read and not used. Make sure not to forget to read and discard them.
Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
long long N, K, M, L;
// 入力読み込み
scanf("%lld %lld %lld %lld", &N, &K, &M, &L);
for(long long i = 0; i < M; i++){
long long d;
scanf("%lld", &d); // 使用しないが読み飛ばす
}
long long ans = N * L;
printf("%lld\n", ans);
return 0;
}
This editorial was generated by claude4.8opus-high.
posted:
last update: