题干:

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. mysql 基础篇(二) 账号、权限管理
  2. 【408预推免复习】计算机组成原理之计算机的发展及应用
  3. 文计笔记7:HTML与CSS
  4. 一切成功的秘诀都蕴含在这个公式里面
  5. 209. 单点登录(SSO)
  6. SAP OData batch request的并行处理实现原理
  7. (转)yi_meng linux 下 ifcfg-eth0 配置 以及ifconfig、ifup、ifdown区别
  8. 【渝粤题库】陕西师范大学202081 管理学 作业 (专升本、高起本、高起专)
  9. 微信公众号 自定义菜单栏目
  10. 实战Veeam Backup Replication 9.5 Upgrade to Veeam Backup Replication 10
  11. Linux添加/删除用户和用户组(linux中,添加cvs用户,实质就是添加linux用户。)
  12. Python实现快乐的数字
  13. 数字核心 驱动转型:SAP S/4HANA 数字化转型论坛 - 杭州站 即刻报名
  14. 计算机形导论形考作业答案,计算机导论形考
  15. 配置管理计划的新设想
  16. 流水灯程序总结:关于Crol和cror的用法
  17. php写phalapi,用PHP搭建你的云平台-PhalApi Pro框架介绍
  18. APM::Rover下GCS_MAVLink的逻辑梳理
  19. IDEA配置Tomcat时总是提示:Error running ‘Tomcat 8.5.73‘ 问题
  20. html钢琴块游戏源码,好玩的钢琴块游戏

热门文章

  1. 关于CNN的权重共享,CNN到底学到了什么?
  2. AtCoder-2379 - 连接竹竿 思维 | 数学
  3. POJ-1651 Multiplication Puzzle 矩阵连乘问题(区间dp)
  4. python中split啥意思_python中split的用法详解_后端开发
  5. c#日期转换周几_Java时间与日期
  6. oracle .dbf文件过大_学习这篇Oracle数据库文件坏块损坏的恢复方法,拓展你的知识面...
  7. C++中的IPv6网络程序设计
  8. xLite连接asterisk提示sip408错误
  9. Ubuntu10.10的网络配置
  10. socket 编程入门教程(一)TCP server 端:5、创建监听嵌套字