题干:

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 energyinstead 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个点,主人公在1号点,现在假设相邻点之间的距离为1(表述为),并且每个点都有一条到其他点的距离为1的捷径(可以是自己,也可以是相邻点,也可以是远处的点,但是这条边是单向边)。定义了一下两点之间最短路的定义,求1号点到其他点的最短距离是多少?

解题报告:

话说啊作为cf div2的B题,考最短路过分了啊虽然是裸题,,但是作为B题,,代码超50行了过分了啊。。

AC代码:

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int MAX = 2e5 + 5;
ll dis[MAX];
bool vis[MAX];
int n,cnt;
vector<ll> vv[MAX];
struct Point {int pos;ll c;Point(){}Point(int pos,ll c):pos(pos),c(c){}bool operator < (const Point & b) const{return c > b.c;}
} ;
void Dijkstra() {for(int i = 1; i<=n; i++) dis[i] = 0x3f3f3f3f3f3f;memset(vis,0,sizeof vis);dis[1] = 0;priority_queue<Point> pq;pq.push(Point(1,0));while(!pq.empty()) {Point cur = pq.top();pq.pop();if(vis[cur.pos]) continue;vis[cur.pos] = 1;int up = vv[cur.pos].size();for(int i = 0; i<up; i++) {int v = vv[cur.pos][i];if(vis[v]) continue;if(dis[v] > dis[cur.pos] + 1) {dis[v] = dis[cur.pos] + 1;pq.push(Point(v,dis[v]));}}}
}
int main()
{cin>>n;int tmp;memset(head,-1,sizeof head);for(int i = 1; i<=n; i++) {scanf("%d",&tmp);vv[i].pb(tmp);}for(int i = 1; i<=n; i++) {if(i != 1) vv[i].pb(i-1);if(i != n) vv[i].pb(i+1);}Dijkstra();for(int i = 1; i<=n; i++) {printf("%lld%c",dis[i],i == n ? '\n' : ' ');}return 0 ;}

【CodeForces - 689B】Mike and Shortcuts(Dijkstra最短路,或者bfs跑状态类似spfa)相关推荐

  1. CodeForces 689B Mike and Shortcuts (bfs or 最短路)

    Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently ...

  2. codeforces 689B Mike and Shortcuts 最短路

    题目大意:给出n个点,两点间的常规路为双向路,路长为两点之间的差的绝对值,第二行为捷径,捷径为单向路(第i个点到ai点),距离为1.问1到各个点之间的最短距离. 题目思路:SPFA求最短路 #incl ...

  3. 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 ...

  4. 坐在马桶上看算法:Dijkstra最短路算法

                                                             [坐在马桶上看算法]算法7:Dijkstra最短路算法 上周我们介绍了神奇的只有五行的 ...

  5. Dijkstra 最短路

    #include <iostream> #define INF 9999999 using namespace std;int main() {int e[51][51],book[50] ...

  6. CodeForces - 798B Mike and strings

    B. Mike and strings time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...

  7. 迪克斯特拉算法(Dijkstra 最短路算法)(简单易懂)

    Dijkstra 最短路算法 上周我们介绍了神奇的只有五行的 Floyd 最短路算法,它可以方便的求得任意两点的最短路径,这称为"多源最短路".本周来来介绍指定一个点(源点)到其余 ...

  8. CodeForces - 1486E Paired Payment(分层图最短路)

    题目链接:点击查看 题目大意:给出一个 nnn 个点 mmm 条边组成的带权无向图,规定每次只能走两条边,假设走的两条边为 a−>b−>ca->b->ca−>b−> ...

  9. 1111 Online Map (30 分)【难度: 一般 / 知识点: Dijkstra最短路】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805358663417856 很传统的最短路,不过要跑两次,其实分开来的话 ...

最新文章

  1. 技术图文:01 面向对象设计原则
  2. [C# 网络编程系列]专题十一:实现一个基于FTP协议的程序——文件上传下载器...
  3. 2018 NLP圣经《自然语言处理综述》最新手稿已经发布!
  4. 【收藏】keepalived配置文件解读
  5. c++调用dll动态链接库历程
  6. linux中的信号1——信号概述
  7. 链表的插入、删除、排序的程序
  8. fopen与读写的标识r,r+,rb+,rt+,w+.....
  9. 沈向洋、王海峰候选中国工程院院士!计算机领域7位入围增选
  10. NSTimer实现读秒、倒计时等周期性操作
  11. pthread异步_异步管道的实现
  12. android获取网络视频缩略图,Android 获取视频(本地和网络)缩略图的解决方案
  13. 如何用python做模型_python的模型如何使用?
  14. 29.yii2 RBAC
  15. SilverLight学习之基本图形
  16. 如何从论文中挖掘和研究思路的办法
  17. creo绘图属性模板_creo绘图属性
  18. 小程序轮播图swiper补充
  19. git reset --hard HEAD~X误删恢复操作
  20. 篮球赛日程表_横县校椅青桐2019春节篮球赛火热开赛!快收好赛程表!

热门文章

  1. [Leedcode][JAVA][第45题][跳跃游戏 II][贪心算法]
  2. 7-13 部落 (25 分)
  3. 预算里怎样计算机械作业费,用实物法编制施工图预算的完整步骤有( )等。 A.计算工程量B.套用预算人工、材料、机械 - 作业在线问答...
  4. solr 7 mysql导入_solr 7.7.0 windows 导入mysql数据库数据
  5. C++求复数的角度_人教A版高中数学必修二7.1 复数的概念优质课公开课课件、教案...
  6. b站电脑客户端_如何将B站的flv格式的视频转换成mp4格式
  7. 计算机控制的点火系统由,第八节(点火系统)
  8. 依赖注入原理 php,PHP依赖注入原理与用法分析
  9. LSGO软件技术团队2015~2016学年第四周(0921~0927)总结
  10. 【转】DICOM文件格式剖析(初识)