E - 燃料補給の旅 / Journey of Refueling Editorial by admin Editorial for printing
gpt-5.5-highOverview
In this problem, towns are arranged in a straight line, and we need to travel from Town \(1\) to Town \(N\) while buying gasoline at different prices in each town.
We can find the minimum cost using a greedy approach: “buy only the minimum necessary amount to reach a cheaper town” and “if there is no cheaper town nearby, buy as much as possible.”
Analysis
First, if there is some interval \(i\) where \(D_i > T\), it is impossible to reach the destination because we cannot travel from Town \(i\) to Town \(i+1\) even with a full tank.
In this case, we immediately output -1.
The key idea is to look at “where the next cheaper gas station is” when we are at Town \(i\).
Let \(C_i\) be the price at Town \(i\).
1. When there is a cheaper town within the tank capacity
If there is a cheaper town \(j\) and the distance from Town \(i\) to Town \(j\) is at most \(T\), it is sufficient to buy just enough fuel to reach Town \(j\).
This is because Town \(j\) is cheaper than Town \(i\), so buying extra gasoline at Town \(i\) for use after Town \(j\) would be a waste of money.
In other words, in this case, we only need to have:
\[ \text{amount needed to reach Town } j \]
in the tank.
2. When there is no cheaper town within the tank capacity
If there is no cheaper town within the tank capacity, we cannot buy gasoline at a price lower than Town \(i\)’s price for a while.
Therefore, it is optimal to buy as much as possible at Town \(i\).
However, if the remaining distance to the destination is less than \(T\), it is sufficient to buy just enough to reach the destination.
In other words, in this case, we fill the tank up to:
\[ \min(T, \text{remaining distance to the destination}) \]
A naive search for the “next cheaper town” for each town would take \(O(N^2)\) time in the worst case.
Since \(N \leq 5 \times 10^5\), this will not run in time.
Therefore, we use a monotonic stack to find the “next town cheaper than the current town” for all towns in \(O(N)\) time.
Algorithm
First, we calculate the position of each town.
Let the position of Town \(1\) be \(0\) and the position of Town \(i\) be \(P_i\). Then:
\[ P_i = D_1 + D_2 + \cdots + D_{i-1} \]
Next, for each town \(i\), we find the first town \(j\) to its east such that:
\[ C_j < C_i \]
Let this town be nxt[i].
This can be computed by scanning from east to west (right to left) using a monotonic stack.
- The stack stores “towns that could be candidates in the future”.
- Towns with prices greater than or equal to the current town \(i\) are removed from the stack.
- They are unnecessary because they are further than Town \(i\) and not cheaper.
- The top of the stack will be the “next cheaper town” from Town \(i\).
After that, we simulate the journey from west to east (left to right).
Let fuel be the current amount of gasoline in the tank.
When we are at Town \(i\):
- If
nxt[i]exists and the distance to it is at most \(T\), we set the target fuel amount to the amount needed to reach that town. - Otherwise, we set the target fuel amount to a full tank or the amount needed to reach the final destination.
Specifically, the target amount target is determined as follows:
- If the next cheaper town \(j\) is within the tank capacity:
\[ target = P_j - P_i \]
- Otherwise:
\[ target = \min(T, P_N - P_i) \]
If the current gasoline amount fuel is less than target, we buy the shortage at the price of Town \(i\).
\[ \text{amount purchased} = target - fuel \]
After purchasing, we consume \(D_i\) liters to move to Town \(i+1\).
For example, suppose the tank capacity is \(10\), the prices are:
\[ [5, 7, 3] \]
and the distances are:
\[ [4, 4] \]
From Town \(1\), the next cheaper town is Town \(3\), and the distance to it is \(8\).
Since this is within the tank capacity of \(10\), it is sufficient to buy only \(8\) liters at Town \(1\).
Town \(2\) is more expensive than Town \(1\), so we do not need to buy any gasoline there.
As a result, we buy \(8\) liters at Town \(1\) for \(5\) yen per liter, making the minimum total cost \(40\) yen.
Complexity
- Time Complexity: \(O(N)\)
- Space Complexity: \(O(N)\)
Implementation Points
If there is any \(D_i > T\), it is impossible to reach the destination, so check this first.
To calculate distances, store the positions of the towns
posusing prefix sums.nxtis computed using a monotonic stack.The answer can be very large. While this is not an issue in Python, you must use
long longin C++.Since we do not move after arriving at Town \(N\), simulating from Town \(1\) to Town \(N-1\) is sufficient.
Source Code
import sys
def main():
input = sys.stdin.buffer.readline
N, T = map(int, input().split())
C = list(map(int, input().split()))
D = list(map(int, input().split()))
if max(D) > T:
print(-1)
return
pos = [0] * N
s = 0
for i, d in enumerate(D, 1):
s += d
pos[i] = s
total_dist = s
nxt = [N] * N
stack = []
for i in range(N - 1, -1, -1):
ci = C[i]
while stack and C[stack[-1]] >= ci:
stack.pop()
if stack:
nxt[i] = stack[-1]
stack.append(i)
ans = 0
fuel = 0
for i in range(N - 1):
j = nxt[i]
if j != N and pos[j] - pos[i] <= T:
target = pos[j] - pos[i]
else:
rem = total_dist - pos[i]
target = T if T < rem else rem
if fuel < target:
buy = target - fuel
ans += buy * C[i]
fuel = target
fuel -= D[i]
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.5-high.
posted:
last update: