B - Mike and Shortcuts

Description

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 ito 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 spendonly 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 1to intersection i.

Sample Input

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 

Hint

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个城市排成一排,起点是第一个城市,每次可以向左或者右相邻城市走,路程为1

每个城市有一个捷径ai 表示第i个城市可以直接到ai城市,路程为1.

问从第一个城市出发,到达每个城市的最短路。

分析:相当于每个城市有三条路可以选择,直接用bfs求最短路径。

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
int a[MAXN],b[MAXN],q[MAXN],d[MAXN];
int main()
{
int n,r=,l=; memset(q,,sizeof(q));
memset(b,0x6f,sizeof(b));
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
b[]=;q[]=;d[++r]=;
for(;l<=r;l++)
{
if(d[l]>&&!q[d[l]-])
{
q[d[l]-]=;
b[d[l]-]=b[d[l]]+;
d[++r]=d[l]-;
}
if(d[l]<n&&!q[d[l]+])
{
q[d[l]+]=;
b[d[l]+]=b[d[l]]+;
d[++r]=d[l]+;
} if(!q[a[d[l]]])
{
q[a[d[l]]]=;
b[a[d[l]]]=b[d[l]]+;
d[++r]=a[d[l]];
}
}
for(int i=;i<n;i++) printf("%d ",b[i]);
printf("%d\n",b[n]);
return ;
}

Codeforces Round #361 (Div. 2) B的更多相关文章

  1. Codeforces Round #361 (Div. 2) C.NP-Hard Problem

    题目连接:http://codeforces.com/contest/688/problem/C 题意:给你一些边,问你能否构成一个二分图 题解:二分图:二分图又称作二部图,是图论中的一种特殊模型. ...

  2. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化 排列组合

    E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...

  3. Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分

    D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...

  4. Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves 二分

    C. Mike and Chocolate Thieves 题目连接: http://www.codeforces.com/contest/689/problem/C Description Bad ...

  5. Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs

    B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...

  6. Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题

    A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...

  7. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】

    任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...

  8. Codeforces Round #361 (Div. 2) D

    D - Friends and Subsequences Description Mike and !Mike are old childhood rivals, they are opposite ...

  9. Codeforces Round #361 (Div. 2) C

    C - Mike and Chocolate Thieves Description Bad news came to Mike's village, some thieves stole a bun ...

随机推荐

  1. Jquary入门( 修改内容)

    1. 使用JQ时需要先引用 JQ 包: 其他的JQ代码 需要写在 引用标签的下面如下图[基本格式] JQ中 是纯代码 没有判断 没有循环   如果 有 时间间隔和延迟  则使用JS 代码 详见 下面例 ...

  2. Java笔记:有啥记啥

    构造方法可以初始化,也可以在调用的时候传参 1android需要java什么水平 2java封装

  3. JS中定义对象原型的两种使用方法

    第一种: function Person() { this.username = new Array(); this.password = "123"; } Person.prot ...

  4. 顺序查找SequentialSearch

    #include <stdio.h>int SequentialSearch(int *a,int n,int x);int main(void){ //num代表查找的数 int num ...

  5. C# Cookie

    1  推荐使用 is 或 as 操作符而不是强制 2  编码风格:Tab ——改成两个 C# 文档注释的快捷键  (将配置表压缩,从压缩文件中查找xml配置表 这个是指Unity项目上面)   尽量使 ...

  6. 【DWR系列06】- DWR日志及js压缩

    img { border: solid 1px } 一.日志 DWR依赖 Apache Commons Logging,可以使用log4j实现日志记录功能. 1.1 日志简介 和其他日志框架一样,当设 ...

  7. [译]:Xamarin.Android开发入门——Hello,Android Multiscreen快速上手

    原文链接:Hello, Android Multiscreen Quickstart. 译文链接:Hello,Android Multiscreen快速上手 本部分介绍利用Xamarin.Androi ...

  8. 传参数应该用哪种形式——值、引用、指针?

    类型:C++ & Qt4,创建时间:十二月 30, 2011, 7:43 p.m. 标题无"转载"即原创文章,版权所有.转载请注明来源:http://hgoldfish.c ...

  9. Python 学习第十七天 jQuery

    一,jQuery 知识详解 利用jquery 查找元素,操作元素 1,jquery 引入 <!DOCTYPE html> <html lang="en"> ...

  10. python爬虫实战

    http://www.jb51.net/article/57161.htm python在线编程:http://www.pythontip.com/coding/run c语言在线编程:http:// ...