303562: CF689B. Mike and Shortcuts

Memory Limit:256 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Mike and Shortcuts

题意翻译

最近,小贝忙于学习,考试和比赛。现在她想出去在城市里逛逛放松一下。 城市有n个路口,从1到n编号。小贝家在1号路口,她从1号路口出发,走过一系列的路口。从路口i到路口j需要消耗|i - j|单位的能量。她走过一系列路口p1p_1p1​ = 1, p2p_2p2​, ..., pkp_kpk​ 消耗的总能量等于![图片](http://codeforces.com/predownloaded/44/8c/448c0e259a30da8dcd84e6438b1754b6cd80a0d3.png) 。 当然,如果没有小路,一味步行是比较无聊的。小路是特别的路径,能让小贝从一个路口到另一个路口只消耗1单位的能量。小贝的城市总共恰好有n条小路,第i条小路允许小贝从路口i走到路口aia_iai​ (i ≤ aia_iai​ ≤ $a_i$$ _ +$$ _ 1$) (但不能反方向走),因此每个路口都有且只有一条小路。严格来说,如果小贝选择一系列路口$p_1$ = 1, $p_2$, ..., $p_k$,那么对于每个1 ≤ i < k 满足$p_i$ + 1 = $a_p$$ _i$,并且$a_p$$ _i$ ≠ $p_i$,从路口|$p_i$到路口$p_i _+ _1$,小贝只会消耗1单位能量,而不是|$p_i$ - $p_i _+ _1$|。 例如,如果小贝选择路口序列$p_1$ = 1, $p_2$ = $a_p$$ _1$, $p_3$ = $a_p$$ _2$, ..., $p_k$ = $a_pk _kk​ _- _1$,她总共消耗k - 1单位的能量。 在她开始闲逛之前,她请你帮她计算从她家到达每个路口最少消耗多少能量。 ### 输入 第一行包含一个整数n (1 ≤ n ≤ 200 000) ,表示小贝城市的路口数量。 第二行包含n个整数$a_1$, $a_2$, ..., $a_n$ (i ≤ $a_i$ ≤ n ,![图片](http://codeforces.com/predownloaded/ea/03/ea038441ad91cbfedeedf4251cf61dea5bea0e3c.png) ,给出小贝城市的小路,允许小贝只消耗1单位能量从路口i到路口$a_i$。请注意小路不能走反方向(从$a_i$到i)。 ### 输出 输出n个整数$m_1,,,m_2$, ..., $m_n$,其中$m_i$表示从路口1到路口i的最小能量花费。

题目描述

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 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF689B/cc97750fb7205e858b8008fe7ee7f7055a90a20f.png) 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&lt;k $ satisfying $ p_{i+1}=a_{pi} $ and $ a_{pi}≠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_{p1},p_{3}=a_{p2},...,p_{k}=a_{pk-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 $ .

输入输出格式

输入格式


The first line contains an integer $ n $ $ (1<=n<=200000) $ — the number of Mike's city intersection. The second line contains $ n $ integers $ a_{1},a_{2},...,a_{n} $ $ (i<=a_{i}<=n $ , ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF689B/ad326ba540ee1b16339ba396d883cf47c32893d3.png), 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 $ ).

输出格式


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 $ .

输入输出样例

输入样例 #1

3
2 2 3

输出样例 #1

0 1 2 

输入样例 #2

5
1 2 3 4 5

输出样例 #2

0 1 2 3 4 

输入样例 #3

7
4 4 4 4 7 7 7

输出样例 #3

0 1 2 1 2 3 3 

说明

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&lt;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

题意翻译

最近,小贝忙于学习,考试和比赛。现在她想出去在城市里逛逛放松一下。 城市有n个路口,从1到n编号。小贝家在1号路口,她从1号路口出发,走过一系列的路口。从路口i到路口j需要消耗|i - j|单位的能量。她走过一系列路口p1p_1p1​ = 1, p2p_2p2​, ..., pkp_kpk​ 消耗的总能量等于![图片](http://codeforces.com/predownloaded/44/8c/448c0e259a30da8dcd84e6438b1754b6cd80a0d3.png) 。 当然,如果没有小路,一味步行是比较无聊的。小路是特别的路径,能让小贝从一个路口到另一个路口只消耗1单位的能量。小贝的城市总共恰好有n条小路,第i条小路允许小贝从路口i走到路口aia_iai​ (i ≤ aia_iai​ ≤ $a_i$$ _ +$$ _ 1$) (但不能反方向走),因此每个路口都有且只有一条小路。严格来说,如果小贝选择一系列路口$p_1$ = 1, $p_2$, ..., $p_k$,那么对于每个1 ≤ i < k 满足$p_i$ + 1 = $a_p$$ _i$,并且$a_p$$ _i$ ≠ $p_i$,从路口|$p_i$到路口$p_i _+ _1$,小贝只会消耗1单位能量,而不是|$p_i$ - $p_i _+ _1$|。 例如,如果小贝选择路口序列$p_1$ = 1, $p_2$ = $a_p$$ _1$, $p_3$ = $a_p$$ _2$, ..., $p_k$ = $a_pk _kk​ _- _1$,她总共消耗k - 1单位的能量。 在她开始闲逛之前,她请你帮她计算从她家到达每个路口最少消耗多少能量。 ### 输入 第一行包含一个整数n (1 ≤ n ≤ 200 000) ,表示小贝城市的路口数量。 第二行包含n个整数$a_1$, $a_2$, ..., $a_n$ (i ≤ $a_i$ ≤ n ,![图片](http://codeforces.com/predownloaded/ea/03/ea038441ad91cbfedeedf4251cf61dea5bea0e3c.png) ,给出小贝城市的小路,允许小贝只消耗1单位能量从路口i到路口$a_i$。请注意小路不能走反方向(从$a_i$到i)。 ### 输出 输出n个整数$m_1,,,m_2$, ..., $m_n$,其中$m_i$表示从路口1到路口i的最小能量花费。

加入题单

算法标签: