codeforces 689bMike and Shortcuts

网友投稿 241 2022-09-02

codeforces 689bMike 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.

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.

图中有一些性质:即 并不需要所有的边都建出因为有很多等效边 所以我们只需要建那些捷径和1~2 2~3 3~4这样的边即可 然后spfa

#include#include#include#define N 220000using namespace std;inline char gc(){ static char now[1<<16],*S,*T; if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;} return *S++;}inline int read(){ int x=0;char ch=gc(); while (ch<'0'||ch>'9') ch=gc(); while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();} return x;}struct node{ int y,next,z;}data[N<<2];int h[N],num,flag[N],n,f[N];void spfa(){ memset(f,0x3f,sizeof(f));queue q;q.push(1);f[1]=0;flag[1]=1; while(!q.empty()){ int x=q.front();q.pop();flag[x]=0; for (int i=h[x];i;i=data[i].next){ int y=data[i].y,z=data[i].z; if (f[x]+z

我从左到右更新 因为左边的答案肯定是由i-1更新过来 右边我们针对所有有捷径的点构造一个单调队列来储存 考虑 什么情况下队首会出队考虑 我当前点是i号点 那么只要我的队尾的点的权值 到i号点 然后新入队的点的权值+(点-i)

#include#include#include#include#define N 220000using namespace std;inline char gc(){ static char now[1<<16],*S,*T; if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;} return *S++;}inline int read(){ int x=0;char ch=gc(); while (ch<'0'||ch>'9') ch=gc(); while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();} return x;}int to[N],n,f[N];deque q;int main(){// freopen("cf.in","r",stdin); n=read();memset(f,0x3f,sizeof(f));f[1]=0; for (int i=1;i<=n;++i){ int to=read(); f[i]=min(f[i],f[i-1]+1);while (!q.empty()&&q.front()<=i) q.pop_front(); if (!q.empty()) f[i]=min(f[i],f[q.front()]+q.front()-i); f[to]=min(f[to],f[i]+1);while (!q.empty()&&f[to]+to<=f[q.back()]+q.back()) q.pop_back();q.push_back(to); } for (int i=1;i<=n;++i) printf("%d ",f[i]); return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:品牌营销:从一见钟情的触电模式到日久生情的抗疲劳模式!
下一篇:bzoj4735 你的生命已如风中残烛
相关文章

 发表评论

暂时没有评论,来抢沙发吧~