(CodeForces - 689B)Mike and Shortcuts

(CodeForces - 689B)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 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.

题目大意:一个城市里有n个十字路口,编号1,2……n,一个人从第i个十字路口到第j个十字路口需要消耗 |ij| 单位的力气,每个十字路口有且仅有一条捷径可以从第i个十字路口到a[i]个十字路口而只需消耗1单位的力气(这条捷径是单向的且 i<=a[i]<=n )。问:一个人从1号十字路口出发,到达每一个十字路口所需的最小消耗的力气为多少。

思路:最短路或bfs。刚好集训学了bfs,且这个题目比较特殊,权值都为1,用bfs求解也比较方便,所以我就用bfs进行了求解。第i个十字路口可以消耗1单位力气到达i-1,i+1或a[i]号十字路口,ans[i]表示到达i号十字路口所需消耗的最小力气。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int maxn=200005;
bool vis[maxn];
int a[maxn],ans[maxn];
int n;

struct node
{
    int x,step;
};

void bfs()
{
    queue<node> q;
    node start,tmp,next;
    start.x=1;
    start.step=0;
    vis[1]=1;
    q.push(start);
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        ans[tmp.x]=tmp.step;
        next.x=tmp.x-1;
        if(next.x>=1&&next.x<=n&&!vis[next.x])
        {
            next.step=tmp.step+1;
            vis[next.x]=1;
            q.push(next);
        }
        next.x=tmp.x+1;
        if(next.x>=1&&next.x<=n&&!vis[next.x])
        {
            next.step=tmp.step+1;
            vis[next.x]=1;
            q.push(next);
        }
        next.x=a[tmp.x];
        if(next.x>=1&&next.x<=n&&!vis[next.x])
        {
            next.step=tmp.step+1;
            vis[next.x]=1;
            q.push(next);
        }
    }
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",a+i);
        memset(vis,0,sizeof(vis));
        bfs();
        for(int i=1;i<=n;i++)
        {
            if(i!=n) printf("%d ",ans[i]);
            else printf("%d\n",ans[i]);
        }
    }
    return 0;
}

在看了别人的bfs代码后发现并不需要结构体,bfs可以写的更为简洁:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int INF=0x3f3f3f3f;
const int maxn=200005;
int a[maxn],f[maxn];
int n; 

void bfs( )
{
    queue<int> q;
    int tmp;
    q.push(1);
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        if(tmp>=1&&tmp<n&&f[tmp+1]>f[tmp]+1)
        {
            f[tmp+1]=f[tmp]+1;
            q.push(tmp+1);
        }
        if(tmp>1&&tmp<=n&&f[tmp-1]>f[tmp]+1)
        {
            f[tmp-1]=f[tmp]+1;
            q.push(tmp-1);
        } 
        if(tmp>=1&&tmp<=n&&f[a[tmp]]>f[tmp]+1)
        {
            f[a[tmp]]=f[tmp]+1;
            q.push(a[tmp]);
        }
    }
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",a+i);
            f[i]=INF;
        }
        f[1]=0;
        bfs();
        for(int i=1;i<=n;i++)
        {
            if(i!=n) printf("%d ",f[i]);
            else printf("%d\n",f[i]);
        }
    }
    return 0;
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值