Permutation Distance
CodeChefOpen on judge ↗
Consider a permutation $P = (p_1, p_2, ..., p_n)$ of numbers $\{1, 2, ... , n\}$. Let us construct a graph $G(P)$ with $n$ vertices as follows: if $i, j \in \{1, ..., n\}$ and $i < j$, then an undirected edge $(i, j)$ is present in $G(P)$ if and only if $p_i < p_j$. Given a permutation $P$, answer several queries of the form "what is the shortest length of a path between $u$ and $v$ in $G(P)$?".
HINT LADDERno hints yet
L1 Observation
L2 Technique
L3 Approach
L4 Pseudo-code
🔒
L5 Full solution
L5 unlocks only if you insist twice
solution.cppC++17
CodeSearch Tutor
Hints, not spoilers — it won’t hand over the full solution unless you insist.
Sign in to chat with the tutor and save your progress.
Sign in to start