Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
来源:互联网 发布:全境封锁ps4网络不好 编辑:程序博客网 时间:2024/05/21 17:14
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 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.
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).
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.
32 2 3
0 1 2
51 2 3 4 5
0 1 2 3 4
74 4 4 4 7 7 7
0 1 2 1 2 3 3
对搜索算法的时间复杂度认识不清晰,看到200000的规模就没敢用搜索算法了,其实由于此题的特殊性完全可用搜索解决
一般说来,对于一个V个顶点,E条边的图来说,搜索算法的时间复杂度为O(V+E)。
对于此题来说,V = n,E最多为2n,所以最后时间复杂度为O(3n),显然是可以胜任的
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <queue>using namespace std;typedef long long LL;const int maxn = 2e5 + 10;int n, a[maxn];int ans[maxn];bool visit[maxn];struct Node {int x, w;Node(int _x, int _w) : x(_x), w(_w) {}};void BFS() {memset(ans, 0, sizeof(ans));memset(visit, false, sizeof(visit));queue<Node> q;q.push(Node(1, 0));while (!q.empty()) {Node head = q.front();q.pop();if (visit[head.x]) continue;ans[head.x] = head.w;visit[head.x] = true;if (!visit[a[head.x]]) {q.push(Node(a[head.x], head.w + 1));}if (!visit[head.x + 1] && head.x + 1 <= n) {q.push(Node(head.x + 1, head.w + 1));}if (!visit[head.x - 1] && head.x - 1 > 1) {q.push(Node(head.x - 1, head.w + 1));}}}int main() {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endifwhile (scanf("%d", &n) != EOF) {for (int i = 1; i <= n; ++i) {scanf("%d", &a[i]);}BFS();for (int i = 1; i <= n; ++i) {printf("%d%c", ans[i], i != n ? ' ' : '\n');}}return 0;}
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
- Codeforces Round #361 (Div. 2) B - Mike and Shortcuts
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts【BFS】
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts【BFS】
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
- Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)
- Codeforces #361 (Div. 2)B-Mike and Shortcuts(spfa最短路)
- Codeforces Round #305 (Div. 2) B.Mike and Fun
- Codeforces Round #305 (Div. 2) B. Mike and Fun(水题)
- Codeforces Round #305 (Div. 2)B. Mike and Fun
- Codeforces Round #305 (Div. 2) C. Mike and Frog +B. Mike and Fun
- CodeForces 689B - Mike and Shortcuts
- codeforces 689B Mike and Shortcuts (bfs)
- CodeForces 689B BFS-Mike and Shortcuts
- day6 CodeForces 689B Mike and Shortcuts
- CodeForces 689B Mike and Shortcuts
- Linux的fasync驱动异步通知详解
- delphi 应用程序开发工具
- json、javaBean、xml互转的几种工具介绍
- unity 调用C++库(android+jni+window dll平台实现)
- 网页
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
- XMPP之删除好友
- 通过对方的请求 获取对方的ip地址
- ios developer tiny share-20160628
- ThinkPHP备份数据( 先在页面列表内显示每个数据表名称,点击操作按钮【备份】对其备份)
- (转)TCP/IP协议三次握手与四次握手流程解析
- WordPress中文插件 Erphpdown vip会员+推广提成+收费下载/查看内容+前端个人中心 银联/支付宝/微信支付/财付通/贝宝paypal[更新至v8.2]
- SSH連現時出現「WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!」解決辦法
- 剑指Offer:面试题23——从上往下打印二叉树(java实现)