codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

689B.md (3752B)


      1 Source: https://codeforces.com/problemset/problem/689/B
      2 
      3 B. Mike and Shortcuts
      4 
      5 time limit per test
      6 
      7 3 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 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://espresso.codeforces.com/b366a738332ae698b6ad24b23eec6b08a25a2fe9.png) units of energy.
     24 
     25 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.
     26 
     27 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*.
     28 
     29 Input
     30 
     31 The first line contains an integer *n* (1 ≤ *n* ≤ 200 000) — the number of Mike's city intersection.
     32 
     33 The second line contains *n* integers *a*1, *a*2, ..., *a**n* (*i* ≤ *a**i* ≤ *n* , ![](https://espresso.codeforces.com/a0e0e50a93f8d64ea6c764b778e4071020d4ef96.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*).
     34 
     35 Output
     36 
     37 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*.
     38 
     39 Examples
     40 
     41 Input
     42 
     43 ```
     44 3  
     45 2 2 3
     46 ```
     47 
     48 Output
     49 
     50 ```
     51 0 1 2
     52 ```
     53 
     54 Input
     55 
     56 ```
     57 5  
     58 1 2 3 4 5
     59 ```
     60 
     61 Output
     62 
     63 ```
     64 0 1 2 3 4
     65 ```
     66 
     67 Input
     68 
     69 ```
     70 7  
     71 4 4 4 4 7 7 7
     72 ```
     73 
     74 Output
     75 
     76 ```
     77 0 1 2 1 2 3 3
     78 ```
     79 
     80 Note
     81 
     82 In the first sample case desired sequences are:
     83 
     84 1: 1; *m*1 = 0;
     85 
     86 2: 1, 2; *m*2 = 1;
     87 
     88 3: 1, 3; *m*3 = |3 - 1| = 2.
     89 
     90 In the second sample case the sequence for any intersection 1 < *i* is always 1, *i* and *m**i* = |1 - *i*|.
     91 
     92 In the third sample case — consider the following intersection sequences:
     93 
     94 1: 1; *m*1 = 0;
     95 
     96 2: 1, 2; *m*2 = |2 - 1| = 1;
     97 
     98 3: 1, 4, 3; *m*3 = 1 + |4 - 3| = 2;
     99 
    100 4: 1, 4; *m*4 = 1;
    101 
    102 5: 1, 4, 5; *m*5 = 1 + |4 - 5| = 2;
    103 
    104 6: 1, 4, 6; *m*6 = 1 + |4 - 6| = 3;
    105 
    106 7: 1, 4, 5, 7; *m*7 = 1 + |4 - 5| + 1 = 3.