Codeforces Round 361 (Div. 2)
B
Mike and Shortcuts
Codeforces
1600
原题
← AC →
翻译中…

Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city.

翻译中…

City consists of $*n*$ intersections numbered from $1$ to $*n*$. Mike starts walking from his house located at the intersection number $1$ and goes along some sequence of intersections. Walking from intersection number $*i*$ to intersection $*j*$ requires $|*i* - *j*|$ units of energy. The total energy spent by Mike to visit a sequence of intersections $*p*_{1} = 1, *p*_{2}, ..., *p*_{*k*}$ is equal to image units of energy.

翻译中…

Of course, walking would be boring if there were no shortcuts. A shortcut is a special path that allows Mike walking from one intersection to another requiring only $1$ unit of energy. There are exactly $*n*$ shortcuts in Mike's city, the $*i*^{*th*}$ of them allows walking from intersection $*i*$ to intersection $*a*_{*i*}$ ($*i* ≤ *a*_{*i*} ≤ *a*_{*i* + 1}$) (but not in the opposite direction), thus there is exactly one shortcut starting at each intersection. Formally, if Mike chooses a sequence $*p*_{1} = 1, *p*_{2}, ..., *p*_{*k*}$ then for each $1 ≤ *i* < *k*$ satisfying $*p*_{*i* + 1} = *a*_{*p*_{*i*}}$ and $*a*_{*p*_{*i*}} ≠ *p*_{*i*}$ Mike will spend only $1$ unit of energy instead of $|*p*_{*i*} - *p*_{*i* + 1}|$ walking from the intersection $*p*_{*i*}$ to intersection $*p*_{*i* + 1}$. For example, if Mike chooses a sequence $*p*_{1} = 1, *p*_{2} = *a*_{*p*_{1}}, *p*_{3} = *a*_{*p*_{2}}, ..., *p*_{*k*} = *a*_{*p*_{*k* - 1}}$, he spends exactly $*k* - 1$ units of total energy walking around them.

翻译中…

Before going on his adventure, Mike asks you to find the minimum amount of energy required to reach each of the intersections from his home. Formally, for each $1 ≤ *i* ≤ *n*$ Mike is interested in finding minimum possible total energy of some sequence $*p*_{1} = 1, *p*_{2}, ..., *p*_{*k*} = *i*$.

翻译中…

Input

翻译中…

The first line contains an integer $*n*$ $(1 ≤ *n* ≤ 200 000)$ — the number of Mike's city intersection.

翻译中…

The second line contains $*n*$ integers $*a*_{1}, *a*_{2}, ..., *a*_{*n*}$ $(*i* ≤ *a*_{*i*} ≤ *n*$ , image, describing shortcuts of Mike's city, allowing to walk from intersection $*i*$ to intersection $*a*_{*i*}$ using only $1$ unit of energy. Please note that the shortcuts don't allow walking in opposite directions (from $*a*_{*i*}$ to $*i*$).

翻译中…

Output

翻译中…

In the only line print $*n*$ integers $*m*_{1}, *m*_{2}, ..., *m*_{*n*}$, where $*m*_{*i*}$ denotes the least amount of total energy required to walk from intersection $1$ to intersection $*i*$.

翻译中…

Note

翻译中…

In the first sample case desired sequences are:

翻译中…

$1: 1$; $*m*_{1} = 0$;

翻译中…

$2: 1, 2$; $*m*_{2} = 1$;

翻译中…

$3: 1, 3$; $*m*_{3} = |3 - 1| = 2$.

翻译中…

In the second sample case the sequence for any intersection $1 < *i*$ is always $1, *i*$ and $*m*_{*i*} = |1 - *i*|$.

翻译中…

In the third sample case — consider the following intersection sequences:

翻译中…

$1: 1$; $*m*_{1} = 0$;

翻译中…

$2: 1, 2$; $*m*_{2} = |2 - 1| = 1$;

翻译中…

$3: 1, 4, 3$; $*m*_{3} = 1 + |4 - 3| = 2$;

翻译中…

$4: 1, 4$; $*m*_{4} = 1$;

翻译中…

$5: 1, 4, 5$; $*m*_{5} = 1 + |4 - 5| = 2$;

翻译中…

$6: 1, 4, 6$; $*m*_{6} = 1 + |4 - 6| = 3$;

翻译中…

$7: 1, 4, 5, 7$; $*m*_{7} = 1 + |4 - 5| + 1 = 3$.

翻译中…

样例

Input #1
3
2 2 3
Output #1
0 1 2
InputCopy OutputCopy
Input #2
5
1 2 3 4 5
Output #2
0 1 2 3 4
InputCopy OutputCopy
Input #3
7
4 4 4 4 7 7 7
Output #3
0 1 2 1 2 3 3
InputCopy OutputCopy
题目信息
题号
689B
时间限制
3 s
内存限制
256 MB
标签
DFS 及类似
图论
贪心
最短路

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。