C - 荷物の搬入 / Loading the Luggage Editorial by admin Editorial for printing
GLM 5.2 (High)Overview
This problem asks for the minimum cost for Takahashi to carry \(N\) items from the warehouse to the rooftop. Although there are landings along the way where items can be placed, the final answer has a very simple form.
Observation
An important observation to solve this problem is to focus on the “distance each item is carried”.
The cost per unit distance Takahashi moves is the “number of items he is carrying at that moment”. In other words, the total cost is equal to the “sum of the distances each item is carried”.
The items must be carried from the warehouse (position \(0\)) to the rooftop (position \(L\)). Therefore, each item must travel a distance of exactly \(L\) in the positive direction (towards the rooftop).
If Takahashi moves back towards the warehouse (in the negative direction) while carrying items, he would be carrying them for an unnecessary extra distance, which would increase the cost. Since moving empty-handed costs \(0\), a smart carrying strategy never requires carrying items in the negative direction.
Therefore, the distance each item is carried in the positive direction is exactly \(L\), meaning each of the \(N\) items is carried for a distance of \(L\). Even if we temporarily place items on intermediate landings or split the transport considering the maximum capacity \(K\) that can be carried at once, the total distance each item travels in the positive direction to reach the rooftop remains \(L\).
Thus, the minimum total cost is \(N \times L\). Although the positions of the landings are given in the input, they do not affect the final answer.
Algorithm
Calculate and output the product of \(N\) and \(L\).
Complexity
- Time Complexity: It takes \(O(M)\) to read the input and \(O(1)\) to compute the answer.
- Space Complexity: \(O(1)\)
Implementation Notes
Since \(N\) and \(L\) can be up to \(10^9\), their product can be up to \(10^{18}\). To prevent integer overflow, you must use a 64-bit integer type (long long in C++) for the calculation.
Source Code
#include <iostream>
using namespace std;
int main() {
long long N, K, M, L;
cin >> N >> K >> M >> L;
for (long long i = 0; i < M; ++i) {
long long d;
cin >> d;
}
cout << N * L << endl;
return 0;
}
This editorial was generated by or-glm5.2-high.
posted:
last update: