For some reason, many of you solve the "Journey" problem very difficult. In fact, this problem has a very simple and short solution.
Let's first learn how to solve a problem when a solution exists (i.e. when the answer is not -1). Imagine that you are driving on the road. You are passing another gas station. Do you need to refuel here? The answer is obvious: if you get to the next gas station on the current stock of gasoline, then you don't have to, otherwise you have to. This algorithm is elementary implemented, for simplicity, it is even better to store not the rest of the gasoline (it is constantly changing), but the coordinate to which we can reach at the current gas station (it changes only when refueling), or, equivalently, information about where we last refueled (the coordinate or number of the gas station).
A slightly alternative approach, but essentially the same — since we are solving the problem, and not driving along the highway, we can always "rewind" time back. Therefore, we drive along the highway, if we see that we did not have enough gasoline before the next gas station, then we "rewind" the time back and decide that we should refuel at the previous gas station.
For ease of implementation, you can add a gas station with the $N$ coordinate (i.e. with the destination coordinate) to the end of the array of gas stations. With any reasonable implementation, you will never refuel in it, but in the main cycle you will not have to particularly consider the last segment of the path.
It remains to learn how to determine when the answer is -1. In general, it is not difficult to understand that the answer is -1 if and only if there are two gas stations in a row, the distance between which is greater than $k$. This can be checked in advance, or you can check immediately during the main cycle, when we decide to refuel, whether we can get to at least the next gas station.
Here are two examples of solutions that implement this simple algorithm:
// Author — Saddambek Nurlanbek uulu, my comments (P.K.) #includeusing namespace std; int k, q, c, n, a[1111]; int main(){ cin >> n >> k >> s; // data entry for(int i = 1;i <= s;++i){ cin >> a[i]; // immediately check if there is a solution if(a[i] - a[i - 1] > k){ cout << -1; return 0; } } // l - the number of the gas station where they refueled last time // (the whole solution, and above too, assumes that in a[0] == 0 initially, // this is actually not very good) int l = 0; // adding refueling at the destination a[++s] = n; for(int i = 1;i <= s;++i){ // if we can't get to the current gas station if(a[i] - a[l] > k){ c++; // refueling in the previous paragraph l = i - 1; } } cout << c; }
# Author — Andrey Efremov, my comments (P.K.) n, k = [int(i) for i in input().split()] s = [int(i) for i in input().split()] # added a gas station "very far away" s.append(2000) ans = 0 # now = what coordinates can we get to with the current supply of gasoline now = k i = 1 # we can't get to the finish line yet while now < n: # if we can't get to the next gas station if s[i + 1] > now: # if we can't get to the current one, then there is no solution if s[i] > now: print(-1) break else: # otherwise, you need to refuel at the current gas station ans += 1 now = s[i] + k i += 1 # else after while in python works only if we didn't exit from while by break else: print(ans)