#P9265. [PA 2022] Chodzenie po linie
[PA 2022] Chodzenie po linie
Problem Description
This problem is translated from PA 2022 Round 5 Chodzenie po linie.
Byteasar is a world-famous circus performer. He is good at walking on tightropes and moving between them. In his famous show, there are tightropes stretched under the circus tent ceiling. If we look at the tent from above and place it on a coordinate plane, the -th tightrope connects the point and the point , where the sequence is a permutation of the integers from to .
Byteasar starts the show standing on one of the tightropes, and the audience gives him the number of some tightrope. His goal is to end up standing on the tightrope chosen by the audience. Byteasar can move very well along a tightrope, but moving from one tightrope to another is quite complicated. Since he is brave but not foolish, he can move from one tightrope to another only when the two tightropes intersect. All tightropes hang at similar heights, so such a move always succeeds, but it is very tiring. For this reason, Byteasar will choose a route that uses the minimum possible number of intersections between different tightropes. The only exception is when it is impossible to reach the target tightrope in this way—in that case, Byteasar will politely thank the audience and return backstage, so he will not cross anything.
However, Byteasar is not sure which tightrope he should start his show on. For each tightrope, he wants to know, over all possible audience choices, the sum of the minimum numbers of intersections he has to pass through. Write a program to compute these values.
Input Format
The first line contains an integer , the number of tightropes.
The second line contains integers , with the meaning described above.
Output Format
Output one line with integers. The -th integer should be the sum, over all possible audience choices, of the minimum number of intersections Byteasar must pass through if he starts from the -th tightrope.
7
2 1 4 7 3 6 5
1 1 9 5 6 7 7
Hint
For of the testdata, it holds that:
.
It is guaranteed that for any , we have .
Translated by ChatGPT 5