B. Mike and Shortcuts

题目连接:

http://www.codeforces.com/contest/689/problem/B

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

Sample Input

3

2 2 3

Sample Output

0 1 2

Hint

题意

你从i到j的代价是abs(j-i)

现在每个点有一条路ai,可以使得i走到ai的花费为1

现在问你从1走到剩下的点的代价是多少

题解

直接bfs就好了,i就只走周围的两个点就好了

代码

#include<bits/stdc++.h>
using namespace std; const int maxn = 2e5+7;
vector<int> E[maxn];
int a[maxn];
int d[maxn];
int vis[maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=0;i<=n;i++)
d[i]=1e9;
queue<int>Q;
Q.push(1);
d[1]=0;
while(!Q.empty()){
int now=Q.front();
Q.pop();
if(now!=n&&d[now+1]>d[now]+1){
d[now+1]=d[now]+1;
Q.push(now+1);
}
if(now!=1&&d[now-1]>d[now]+1){
d[now-1]=d[now]+1;
Q.push(now-1);
}
if(d[a[now]]>d[now]+1){
d[a[now]]=d[now]+1;
Q.push(a[now]);
}
}
for(int i=1;i<=n;i++)
printf("%d\n",d[i]);
printf("\n");
}

最新文章

  1. [Android]一个干净的架构(翻译)
  2. Android 大牛的 blog 值得推荐 (转 整理)
  3. java开发_读写txt文件操作
  4. vim之插入
  5. HighChartS cpu利用率动态图(Java版)
  6. 【S4】使用empty()而不是判断size()是否为0
  7. pop()实现逐个删除数组最后一位并输出
  8. Java邮件服务学习之一:邮件服务概述
  9. bat里如何用相对路径
  10. spring mvc DispatcherServlet详解之拾忆工具类utils
  11. 关于unsigned int和int的加法
  12. table常用
  13. 由世纪互联运营的 Windows Azure 现已在中国正式发布
  14. pyfits过滤数据更新文件。
  15. python布尔类型
  16. 你真的理解 Spring Boot 项目中的 parent 吗?
  17. 用kali执行arp攻击-----------使对方断网
  18. B - Battle City bfs+优先队列
  19. CSS中一个冒号和两个冒号有什么区别
  20. 软件测试_Linux

热门文章

  1. mysql -&gt; 备份与恢复_11
  2. Flask:redirect()函数
  3. 常见四大类型视频接线DP、HDMI、DVI、VGA的比较
  4. 洛谷P2279消防局的设立
  5. 浮动元素垂直居中,bootstrap栅格布局垂直居中
  6. U盘删除文件时提示“文件或目录损坏且无法读取”的解决方法
  7. Spring整合junit测试
  8. Abstract Factory 抽象工厂
  9. Asp.Net Core2.0 WebAPI 使用Swagger生成漂亮的接口文档
  10. sql中的if()和ifnull() 的用法和区别