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 p1?=?1,?p2,?...,?pk 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 1 unit of energy. There are exactly n shortcuts in Mike‘s city, the ith of them allows walking from intersection i to intersection ai (i?≤?ai?≤?ai?+?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,?...,?pk then for each 1?≤?i?<?k satisfying pi?+?1?=?api and api?≠?pi Mike will spend only 1 unit of energy instead of |pi?-?pi?+?1| walking from the intersection pi to intersection pi?+?1. For example, if Mike chooses a sequence p1?=?1,?p2?=?ap1,?p3?=?ap2,?...,?pk?=?apk?-?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 p1?=?1,?p2,?...,?pk?=?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 a1,?a2,?...,?an (i?≤?ai?≤?n , , describing shortcuts of Mike‘s city, allowing to walk from intersection i to intersection ai using only 1 unit of energy. Please note that the shortcuts don‘t allow walking in opposite directions (from ai to i).
Output
In the only line print n integers m1,?m2,?...,?mn, where mi denotes the least amount of total energy required to walk from intersection 1 to intersection i.
Examples
input
3 2 2 3
output
0 1 2
input
7 4 4 4 4 7 7 7
output
0 1 2 1 2 3 3
题目大意
由于每段路径(非捷径)的长度为1,每走过一个长度单位所消耗的能量也是1,可以只考虑长度为一的路径。这样一来,所有路径(包括捷径)都可以一视同仁,
因为每次只前进一步,一步消耗一单位能量。可以采用BFS来边走边标记走到每一点所需的最少能量;
一条大路上的每个节点都有通向前面某个点的捷径,捷径只能向前走,求最短路程。注意捷径虽然是单向的但是大路可以是双向的,每次搜索要判三种情况。
另BFS是适合找最短路,找到即最优;DFS是所有解果都会搜到
代码实现
#include <bits/stdc++.h> using namespace std; int arr[200005]; //每个位置i到1的距离 int shortt[200005];//存储捷径 i->shortt[i] 的距离为1 struct point{ int now; //当前点位 int step; //步数 即距离 }; int main(){ int n; cin>>n; memset(arr,-1,sizeof(arr)); for(int i=1;i<=n;i++) cin>>shortt[i]; queue<point> q; point start; start.now=1,start.step=0; //初始化数据 当前位置 1;距离 0. q.push(start); while(!q.empty()){ //BFS代码 point cur; //cur:下一个 point temp=q.front(); //temp :当前 int now=temp.now; int step=temp.step; q.pop(); if(now+1<=n&&arr[now+1]==-1){ //向右走 arr[now+1]=step+1; //距离加一 cur.now=now+1; cur.step=step+1; //将该点位push进队列 q.push(cur); }if(arr[shortt[now]]==-1){ //若在i这个位置又捷径 直接走捷径 arr[shortt[now]]=step+1; //距离加一 cur.now=shortt[now];cur.step=step+1; q.push(cur); }if(now-1>1&&arr[now-1]==-1){//向左走 arr[now-1]=step+1; cur.now=now-1; cur.step=step+1; q.push(cur); } } arr[1]=0; //1号点是0 for(int i=1;i<=n;i++){ cout<<arr[i]<<" "; } return 0; }