E - 燃料補給の旅 / Journey of Refueling Editorial by admin Editorial for printing
gpt-5.5-highOverview
We want to find the minimum cost to travel from town \(1\) to town \(N\) while refueling such that the fuel in the tank does not exceed the capacity \(T\), given that the gasoline price varies by town.
The basic strategy is a greedy approach: “If there is a town with cheaper gasoline within reach of the current town, buy just enough to reach that town; otherwise, buy as much as possible.”
Observations
First, to move from the adjacent town \(i\) to \(i+1\), we need \(D_i\) liters of fuel.
If \(D_i > T\) for some \(i\), we cannot traverse this segment even with a full tank, making it impossible to reach the destination. In this case, output -1.
Hereafter, we assume \(D_i \leq T\) for all \(i\).
An important observation is to consider the “next town with a cheaper gasoline price than the current town” when we are at town \(i\).
Let \(C_i\) be the price at town \(i\).
1. When a cheaper town is within the tank capacity
If there is a town \(j\) to the east of town \(i\) which is the first town satisfying \(C_j < C_i\), and
\(pos_j - pos_i \leq T\)
then it is sufficient to buy just enough fuel to reach town \(j\).
This is because we can buy fuel cheaper at town \(j\) than at town \(i\), so buying fuel at town \(i\) for use after reaching town \(j\) would be suboptimal.
Also, there is no town cheaper than town \(i\) between town \(i\) and town \(j\). Therefore, we do not lose anything by buying the fuel needed to reach town \(j\) at town \(i\).
Thus, in this case, the target amount of fuel when leaving town \(i\) is
\(pos_j - pos_i\)
2. When there is no cheaper town within the tank capacity
There is no town cheaper than town \(i\) within the reachable range of tank capacity \(T\).
In this case, the gasoline we can buy now is the cheapest, at least within the reachable range. Therefore, it is optimal to buy as much as possible.
However, we do not need to buy more than what is required to reach the destination, so the target fuel amount when leaving town \(i\) is
\(\min(T, pos_N - pos_i)\)
Why the Naive Approach is Difficult
If we search for the “next cheaper town” to the right for each town individually, it would take \(O(N^2)\) time in the worst case.
Since \(N \leq 5 \times 10^5\), this will exceed the time limit.
Also, if we consider a DP with the remaining fuel as a state, it is impractical because \(T \leq 10^9\).
Therefore, we precompute the “next cheaper town” for each town using a monotonic stack.
Algorithm
1. Check Reachability
For all \(D_i\), we must have
\(D_i \leq T\)
If there is even one \(D_i > T\), output -1 and terminate.
2. Calculate the Positions of Each Town
Let the position of town \(1\) be \(pos_1 = 0\).
Then,
\(pos_{i+1} = pos_i + D_i\)
With this, the distance from town \(i\) to town \(j\) can be calculated as
\(pos_j - pos_i\)
3. Find the Next Cheaper Town Using a Monotonic Stack
For each town \(i\), let nxt[i] be the first town \(j\) to the east such that \(C_j < C_i\).
We can find this using a monotonic stack by traversing from right to left.
for (int i = N; i >= 1; i--) {
while (!st.empty() && C[st.back()] >= C[i]) st.pop_back();
if (!st.empty()) nxt[i] = st.back();
else nxt[i] = N + 1;
st.push_back(i);
}
Towns with C[st.back()] >= C[i] are not cheaper than town \(i\), so they are unnecessary. Towns with equal prices are also removed because they are not “strictly cheaper”.
4. Simulate from West to East
Let fuel be the current remaining fuel and ans be the total cost.
Upon arriving at town \(i\), we determine the target amount of fuel target we want to have when leaving.
- If
nxt[i]exists and the distance to it is at most \(T\):
target = pos[nxt[i]] - pos[i] - Otherwise:
target = min(T, pos[N] - pos[i])
If the current remaining fuel is less than target, we buy the deficit at town \(i\).
if (fuel < target) {
long long buy = target - fuel;
ans += buy * C[i];
fuel = target;
}
After that, we consume \(D_i\) liters of fuel to move to town \(i+1\).
fuel -= D[i];
By repeating this process from town \(1\) to town \(N-1\), we can find the minimum cost.
Complexity
- Time Complexity: \(O(N)\)
- Space Complexity: \(O(N)\)
Implementation Points
Since the answer can be up to around \(10^{18}\), use
long long.Although \(C_N\) is provided in the input, we do not need to refuel at town \(N\) because we do not move any further after arriving at town \(N\).
Since towns with the same price are not “cheaper,” we remove them using
>=in the monotonic stack.Note that we do not need to fill up the tank if the destination is close, which is why we use
min(T, pos[N] - pos[i]).Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
long long T;
cin >> N >> T;
vector<long long> C(N + 1);
for (int i = 1; i <= N; i++) cin >> C[i];
vector<long long> D(N);
bool ok = true;
for (int i = 1; i <= N - 1; i++) {
cin >> D[i];
if (D[i] > T) ok = false;
}
if (!ok) {
cout << -1 << '\n';
return 0;
}
vector<long long> pos(N + 1, 0);
for (int i = 1; i <= N - 1; i++) {
pos[i + 1] = pos[i] + D[i];
}
vector<int> nxt(N + 1, N + 1);
vector<int> st;
for (int i = N; i >= 1; i--) {
while (!st.empty() && C[st.back()] >= C[i]) st.pop_back();
if (!st.empty()) nxt[i] = st.back();
st.push_back(i);
}
long long fuel = 0;
long long ans = 0;
for (int i = 1; i <= N - 1; i++) {
long long target;
if (nxt[i] <= N && pos[nxt[i]] - pos[i] <= T) {
target = pos[nxt[i]] - pos[i];
} else {
target = min(T, pos[N] - pos[i]);
}
if (fuel < target) {
long long buy = target - fuel;
ans += buy * C[i];
fuel = target;
}
fuel -= D[i];
}
cout << ans << '\n';
return 0;
}
This editorial was generated by gpt-5.5-high.
posted:
last update: