CSU-ACM2017暑期训练6-bfs Mike and Shortcuts
来源:互联网 发布:微信清死粉软件安卓版 编辑:程序博客网 时间:2024/06/06 02:00
Mike and Shortcuts
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.
Example
Input32 2 3Output0 1 2 Input51 2 3 4 5Output0 1 2 3 4 Input74 4 4 4 7 7 7Output0 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.
由于每段路径(非捷径)的长度为1,每走过一个长度单位所消耗的能量也是1,可以只考虑长度为一的路径。这样一来,所有路径(包括捷径)都可以一视同仁,因为每次只前进一步,一步消耗一单位能量。
自然地,可以采用BFS来边走边标记走到每一点所需的最少能量:
#include <iostream>#include <cstring>#include <queue>#include <cstdio>//#define TESTusing namespace std;int n, arr[200005], shortcut[200004];struct point{ int now, step; point(){} point(int _now, int _step):now(_now), step(_step){}};void bfs(){ queue<point> q; q.push(point(1,0)); while(!q.empty()){ point temp = q.front(); q.pop(); int now = temp.now, step = temp.step; if(now - 1 >= 1 && arr[now - 1] == -1){ //---+ arr[now - 1] = step + 1; // | q.push(point(now - 1, step + 1)); // | } // | if(arr[shortcut[now]] == -1){ // | arr[shortcut[now]] = step + 1; // +--三个方向:左、右、 q.push(point(shortcut[now], step + 1)); // | 通过捷径。消耗较少 } // | 的路径一定先被标记。 if(now + 1 <= n && arr[now + 1] == -1){ // | 所以只需考虑未被标记 arr[now + 1] = step + 1; // | 的。 q.push(point(now + 1, step + 1)); //---+ } }}int main(){#ifdef TESTfreopen("test.txt", "r", stdin);#endif // TEST while(cin >> n){ memset(arr, -1, sizeof(arr)); arr[1] = 0; for(int i = 1; i <= n; i++) scanf("%d", &shortcut[i]); bfs(); for(int i = 1; i <= n; i++) printf("%d ", arr[i]); cout << endl; } return 0;}
阅读全文
0 0
- CSU-ACM2017暑期训练6-bfs Mike and Shortcuts
- CSU-ACM2017暑期训练6-bfs C
- CSU-ACM2017暑期训练6-bfs I
- CSU-ACM2017暑期训练6-bfs H
- CSU-ACM2017暑期训练6-bfs G
- CSU-ACM2017暑期训练6-bfsH
- BFS-Mike and Shortcuts
- CSU-ACM2017暑期训练3 J
- CSU-ACM2017暑期训练4-dfs D
- CSU-ACM2017暑期训练4-dfs E
- CSU-ACM2017暑期训练4-dfs F
- CSU-ACM2017暑期训练4-dfs G
- CSU-ACM2017暑期训练5-三分 E
- CSU-ACM2017暑期训练5-三分 A
- CSU-ACM2017暑期训练5-三分 D
- CSU-ACM2017暑期训练5-三分 E
- CSU-ACM2017暑期训练12-KMP D
- CSU-ACM2017暑期训练12-KMP E
- 生活本就是点点滴滴,没有积累便容易忘记
- 第九章项目人力资源管理
- 数据结构10:二叉树专题
- JavaScript将指定的元素对象移动到固定的距离函数封装
- 在 Linux 系统上安装主播软件
- CSU-ACM2017暑期训练6-bfs Mike and Shortcuts
- 在TP驱动中添加虚拟按键
- 腾讯js面试题3
- 将搜索二叉树转化为双向链表
- 在ListActivity底部添加不动的按钮
- 工作日记2017.08.01
- spring中获取项目根路径
- 操作系统实验三实验报告
- 怎样用异或查找出多出的字母