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 sequencep1 = 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
  1. 3
  2. 2 2 3
output
  1. 0 1 2
input
  1. 5
  2. 1 2 3 4 5
output
  1. 0 1 2 3 4
input
  1. 7
  2. 4 4 4 4 7 7 7
output
  1. 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.

这题基本不能用SPFA,20W个点,边多到存不下,SPFA会WA4,然后试了下BFS,也WA4,发现有个坑,第四组中到第10个点的最短距离是8,是1走捷径到17,再一步一步往回走到10即1+7=8而不是直接从1走到10的直接距离9,显然这里需要往回走。BFS里面多写一个往回走的方向搜索就好了

代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdlib>
  4. #include<sstream>
  5. #include<cstring>
  6. #include<cstdio>
  7. #include<string>
  8. #include<deque>
  9. #include<stack>
  10. #include<cmath>
  11. #include<queue>
  12. #include<set>
  13. #include<map>
  14. using namespace std;
  15. #define INF 0x3f3f3f3f
  16. #define MM(x,y) memset(x,y,sizeof(x))
  17. typedef pair<int,int> pii;
  18. typedef long long LL;
  19. const double PI=acos(-1.0);
  20. const int N=200010;
  21.  
  22. int vis[N];
  23. int cut[N];
  24. int d[N];
  25. queue<pii>Q;
  26.  
  27. void init()
  28. {
  29. MM(vis,0);
  30. MM(cut,-1);
  31. MM(d,0);
  32. while (!Q.empty())
  33. Q.pop();
  34. }
  35. void bfs(const int &s)
  36. {
  37. Q.push(pii(s,0));
  38. while (!Q.empty())
  39. {
  40. pii now=Q.front();
  41. Q.pop();
  42. vis[now.first]=1;
  43. d[now.first]=now.second;
  44. int v;
  45. v=cut[now.first];
  46. if(!vis[v]&&v!=-1)
  47. {
  48. Q.push(pii(v,now.second+1));
  49. vis[v]=1;
  50. }
  51. v=now.first+1;
  52. if(!vis[v])
  53. {
  54. Q.push(pii(v,now.second+1));
  55. vis[v]=1;
  56. }
  57. v=now.first-1;
  58. if(!vis[v])
  59. {
  60. Q.push(pii(v,now.second+1));
  61. vis[now.first-1]=1;
  62. }
  63. }
  64. return ;
  65. }
  66. int main(void)
  67. {
  68. int n,i,j,a,b;
  69. while (~scanf("%d",&n))
  70. {
  71. init();
  72. for (i=1; i<=n; i++)
  73. {
  74. scanf("%d",&a);
  75. cut[i]=a;
  76. }
  77. bfs(1);
  78. for (i=1; i<=n; i++)
  79. printf("%d%s",d[i],i==n?"\n":" ");
  80. }
  81. return 0;
  82. }

Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)的更多相关文章

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

  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) C. Mike and Chocolate Thieves 二分

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

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

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

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

  6. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化+逆元

    E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input ...

  7. Codeforces Round #361 (Div. 2)A. Mike and Cellphone

    A. Mike and Cellphone time limit per test 1 second memory limit per test 256 megabytes input standar ...

  8. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem

    题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt ...

  9. set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

    题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...

随机推荐

  1. Oracle Data Integrator 12c 安装(ODI安装)

    Oracle Data Integrator 12c 安装(ODI安装) 企业版安装步骤(包含独立安装步骤) 官网下载Oracle Data Integrator 12cR2 (12.2.1.0.0) ...

  2. LightOJ 1422 Halloween Costumes (区间DP,经典)

    题意: 有个人要去参加万圣节趴,但是每到一个趴都要换上特定的服装,给定一个序列表示此人要穿的衣服编号(有先后顺序的),他可以套很多件衣服在身上,但此人不喜欢再穿那些脱下的衣服(即脱下后就必须换新的), ...

  3. FTP的环境搭建和防火墙设置

    步骤: 1.右键点击无线网--->打开网络和共享中心--->控制面板--->程序--->启用或关闭Wondows功能

  4. (九)mybatis之生命周期

    生命周期   SqlSessionFactoryBuilder   SqlSessionFactoryBuilder的作用就是生成SqlSessionFactory对象,是一个构建器.所以我们一旦构建 ...

  5. (九)maven之聚合多模块

    聚合项目 一些开源项目,都会把自己的源代码公开到github之类的网站上,我们通过下载其代码,在本地执行maven install,可以把代码编译成jar包安装到本地仓库.而一个项目通常有多个模块,比 ...

  6. License开源许可协议

    开源许可协议 License是软件的授权许可,表述了你获得代码后拥有的权利,可以对别人的作品进行何种操作,何种操作又是被禁止的. 开源许可证种类 Open Source Initiative http ...

  7. java 核心技术卷一笔记 6 .1接口 lambda 表达式 内部类

    6.1 接口不是类,是对类的一组需求的描述,这些类需要遵守接口描述的统一格式进行定义.例如:Arrays类中sort方法(可以对对象数组进行排序)前提是对象所属的类必须实现了Comparable 接口 ...

  8. 创建一个文件夹用于写入UTF-8编码的文件

    实现效果: 知识运用: File类的CreateText方法 StreamWriter类的WriteLine方法 实现代码: private void button2_Click(object sen ...

  9. java反序列化字节转字符串工具

    https://github.com/NickstaDB/SerializationDumper SerializationDumper-v1.1.jar 用法 : java -jar Seriali ...

  10. 高德定位腾讯定位在APP上无法开启定位权限的解决方案

    [备注]公司项目中遇到的问题,如果你在团队工作其中定有不少配合方面的问题,其中的思路是可以借鉴的,因为这也许正是你们现在遇到的问题,总结的不好的地方还请多多指教 因为项目需求的确定,定位成了必不可少的 ...