Teleportation Points (APCS 2019-10 Advanced)
2.0s 256MPositions are \(0..n-1\). Start at \(0\) and reach \(P\). One move first goes from \(u\) to \(v=u-L\) or \(u+R\), then immediately teleports to \(S_v\). Both positions must be in range. This entire action costs one move and triggers exactly one teleport, without chaining. Find the minimum moves.
Input
Read \(n,P,L,R\), then \(S_0\) through \(S_{n-1}\).
Constraints
\(2\le n\le10^6\), \(0\le P<n\), \(1\le L,R\le\lfloor n/2\rfloor\), \(|S_i|\le10^8\), \(S_0=0\), \(S_P=P\).
Output
Print the minimum moves, \(-1\) if unreachable, or \(0\) if already at the target.
Sample Input
5 3 1 2
0 2 4 3 1
Sample Output
2
Source
Log in to write and submit code.
Log in