【CodeForces-689B】解题报告(BFS)

原始题目

B. Mike and Shortcuts

  • time limit per test 3 seconds

  • memory limit per test 256 megabytes

  • input standard input

  • output standard output

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 energyspent 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

5
1 2 3 4 5

output

0 1 2 3 4 

input

7
4 4 4 4 7 7 7

output

0 1 2 1 2 3 3 

Note

In the first sample case desired sequences are:

1: 1; m1 = 0;

2: 1, 2; m2 = 1;

3: 1, 3; m3 = |3 - 1| = 2.

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

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

1: 1; m1 = 0;

2: 1, 2; m2 = |2 - 1| = 1;

3: 1, 4, 3; m3 = 1 + |4 - 3| = 2;

4: 1, 4; m4 = 1;

5: 1, 4, 5; m5 = 1 + |4 - 5| = 2;

6: 1, 4, 6; m6 = 1 + |4 - 6| = 3;

7: 1, 4, 5, 7; m7 = 1 + |4 - 5| + 1 = 3.

题目大意

给出n个点,如果直接从a1点到an点,耗费|xn-x1|的能量,不过每个点都存在一个到另一个点的“快速路径”,消耗能量为1,现在求从第一个点开始到每一个点消耗能量的最小值。

解题思路

广搜,下一次搜索的规则是,左右相邻的点和该点快速路径到达的点(即消耗能量均为1的点),三个方向,这样一层一层往下搜索,第一次到达ai点时消耗的能量(计数器)即为从第一个点到该点消耗的最小能量。

解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#define maxn 200010
using namespace std;
int n;//个数
bool vis[maxn]; // 访问标记
//int dir[4][2]={0,1,0,-1,1,0,-1,0}; // 方向向量
//int dir[8][2]={{1,1},{1,0},{1,-1},{0,1},{0,-1},{-1,1},{-1,0},{-1,-1}} ;
int dir[3];
struct State // BFS 队列中的状态数据结构
{
int i;
int ai;
int Step_Counter; // 搜索步数统计器
bool first;
int min;
};

State a[maxn];
int ans[maxn];

bool CheckState(State next) // 约束条件检验
{
if(next.i>0&&next.i<=n&&!vis[next.i]) // 满足条件
{
// printf("next %d %d\n",next.i,next.ai);
return 1;
}
else // 约束条件冲突
return 0;
}

void bfs(State st)
{
queue <State> q; // BFS 队列
State now,next; // 定义2个状态,当前和下一个
st.Step_Counter=0; // 计数器清零
q.push(st); // 入队
vis[st.i]=1; // 访问标记
int circle=0;
while(!q.empty())
{
now=q.front(); // 取队首元素进行扩展
// a[now.i].Step_Counter=++circle;
if(a[now.i].first) // 出现目标态,此时为Step_Counter 的最小值,可以退出即可
{
// 做相关处理
// printf("first visit i=%d, min=%d\n",now.i,now.Step_Counter);
a[now.i].min=now.Step_Counter;
a[now.i].first=false;
// return;
}
else if(a[now.i].min<now.Step_Counter)
a[now.i].min=now.Step_Counter;
dir[0]=now.i-1;dir[1]=now.i+1;dir[2]=now.ai; //生成下一组方向
for(int i=0;i<3;i++)
{
next.i=dir[i];// 按照规则生成 下一个状态
next.ai=a[dir[i]].ai;
next.Step_Counter=now.Step_Counter+1; // 计数器加1
if(CheckState(next)) // 如果状态满足约束条件则入队
{
q.push(next);
// printf("insert i=%d ai=%d\n",next.i,next.ai);
vis[next.i]=1; //访问标记
}
}
q.pop(); // 队首元素出队
}
return;
}

int main()
{
while(~scanf("%d",&n))
{
memset(vis,0,sizeof(vis));
for(int j=1;j<=n;j++)
{
a[j].i=j;
a[j].first=true;
scanf("%d",&a[j].ai);
}
bfs(a[1]);
int flag=1;
for(int j=1;j<=n;j++)
{
if(flag)
{
printf("%d",a[j].min);
flag=0;
}
else
printf(" %d",a[j].min);
}
printf("\n");
}
return 0;
}

收获与反思

思路清楚的话还是比较好写的,这里没有绝对的目标状态,所以全部遍历一遍每一个都打上Step值,然后输出就行。