CF689B.Mike and Shortcuts

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 nn intersections numbered from 11 to nn . Mike starts walking from his house located at the intersection number 11 and goes along some sequence of intersections. Walking from intersection number ii to intersection jj requires ij|i-j| units of energy. The total energy spent by Mike to visit a sequence of intersections p1=1,p2,...,pkp_{1}=1,p_{2},...,p_{k} is equal to 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 11 unit of energy. There are exactly nn shortcuts in Mike's city, the ithi^{th} of them allows walking from intersection ii to intersection aia_{i} ( i<=ai<=ai+1i<=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 p1=1,p2,...,pkp_{1}=1,p_{2},...,p_{k} then for each 1<=i<k satisfying pi+1=apip_{i+1}=a_{pi} and apipia_{pi}≠p_{i} Mike will spend only 11 unit of energy instead of pipi+1|p_{i}-p_{i+1}| walking from the intersection pip_{i} to intersection pi+1p_{i+1} . For example, if Mike chooses a sequence p1=1,p2=ap1,p3=ap2,...,pk=apk1p_{1}=1,p_{2}=a_{p1},p_{3}=a_{p2},...,p_{k}=a_{pk-1} , he spends exactly k1k-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<=n1<=i<=n Mike is interested in finding minimum possible total energy of some sequence p1=1,p2,...,pk=ip_{1}=1,p_{2},...,p_{k}=i .

输入格式

The first line contains an integer nn (1<=n<=200000)(1<=n<=200000) — the number of Mike's city intersection.

The second line contains nn integers a1,a2,...,ana_{1},a_{2},...,a_{n} (i<=ai<=n(i<=a_{i}<=n , , describing shortcuts of Mike's city, allowing to walk from intersection ii to intersection aia_{i} using only 11 unit of energy. Please note that the shortcuts don't allow walking in opposite directions (from aia_{i} to ii ).

输出格式

In the only line print nn integers m1,m2,...,mnm_{1},m_{2},...,m_{n} , where mim_{i} denotes the least amount of total energy required to walk from intersection 11 to intersection ii .

输入输出样例

  • 输入#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:11:1 ; m1=0m_{1}=0 ;

2:1,22:1,2 ; m2=1m_{2}=1 ;

3:1,33:1,3 ; m3=31=2m_{3}=|3-1|=2 .

In the second sample case the sequence for any intersection 1<i is always 1,i1,i and mi=1im_{i}=|1-i| .

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

1:11:1 ; m1=0m_{1}=0 ;

2:1,22:1,2 ; m2=21=1m_{2}=|2-1|=1 ;

3:1,4,33:1,4,3 ; m3=1+43=2m_{3}=1+|4-3|=2 ;

4:1,44:1,4 ; m4=1m_{4}=1 ;

5:1,4,55:1,4,5 ; m5=1+45=2m_{5}=1+|4-5|=2 ;

6:1,4,66:1,4,6 ; m6=1+46=3m_{6}=1+|4-6|=3 ;

7:1,4,5,77:1,4,5,7 ; m7=1+45+1=3m_{7}=1+|4-5|+1=3 .

首页