[Solution] Permutation Graph Codeforces Solution | Codeforces Problem Solution 2022
A permutation is an array consisting of distinct integers from to in arbitrary order. For example, is a permutation, but is not a permutation ( appears twice in the array) and is also not a permutation ( but there is in the array).
You are given a permutation of , . For integers , such that , define as , and define as .
Let us build an undirected graph of vertices, numbered to . For every pair of integers , if and both holds, or and both holds, add an undirected edge of length between vertices and .
In this graph, find the length of the shortest path from vertex to vertex . We can prove that and will always be connected via some path, so a shortest path always exists.
Each test contains multiple test cases. The first line contains the number of test cases (). Description of the test cases follows.
The first line of each test case contains one integer ().
The second line of each test case contains integers , , , (). It's guaranteed that is a permutation of , , , .
It is guaranteed that the sum of over all test cases does not exceed .
For each test case, print a single line containing one integer — the length of the shortest path from to .
No comments:
Post a Comment