Minimum cost
Practice
3 (43 votes)
Algorithms
Breadth first search
Easy
Graphs
Problem
87% Success 3223 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.
Explanation
-
Code Editor
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Submissions
Please login to view your submissions
Similar Problems
Points:30
444 votes
Tags:
Breadth First SearchDepth First SearchDynamic ProgrammingEasyGraphsMathNumber Theory
Points:30
61 votes
Tags:
Ad-HocAlgorithmsBreadth First SearchEasyGraphs
Points:30
13 votes
Tags:
ApprovedBreadth First SearchEasyGraphs
Editorial
Login to unlock the editorial
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor