Minimum cost
Practice
3 (43 votes)
Algorithms
Breadth first search
Easy
Graphs
Problem
87% Success 3219 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code
You are standing at position \(1\). From position $$i$$, you can walk to $$i+1$$ or $$i-1$$ with cost \(1\). From position \(i\), you can travel to without any cost to $$p_i$$ (\(p\) is a permutation of numbers \(1 \ldots n\)). You have to reach position \(n\). Determine the minimum possible cost.
Input format
- First line: \(T\) denotes the number of test cases (\(1 \le T \le 10\))
- For each test case:
- First line: \(n\) (\(1 \le n \le 50\ 000\))
- Second line: \(n\) integers where the \(i_{th}\) integer is \(p_i\)
Note: It is guaranteed that \(p\) is a permutation.
Output format
Print the minimum possible cost.
Submissions
Please login to view your submissions
Similar Problems
1.Graphs
Points:30
14 votes
Tags:
AlgorithmsBreadth First SearchC++Depth First SearchEasyGraphs
Points:30
61 votes
Tags:
Ad-HocAlgorithmsBreadth First SearchEasyGraphs
Points:30
444 votes
Tags:
Breadth First SearchDepth First SearchDynamic ProgrammingEasyGraphsMathNumber Theory
Editorial