Jumping

滴答滴答--题目链接

Shopping with the wife is one of men's hardest marriage responsibilities. Nour is a good husband. So he goes out with his wife every Friday to "Damasquino" mall.

Damasquino is a very famous mall in Damascus. It has a very unique transportation method between shops. Since the shops in the mall are laying in a straight line, you can jump on a very advanced trampoline from the shop i, and land in shop (i + di) or (i - di) depending on the direction of the jump. Where di is a constant given for each shop. You can jump forward or backward only, and you can't land any where before the first shop or after the last shop in the mall.

There are N shops in the mall, numbered from 1 to N. Nour's wife starts her shopping journey from shop 1 and ends it in shop N. Unfortunately, Nour sometimes gets lost from his wife (the mall is very crowded on Fridays). So he wants to know for each shop, what is the minimum number of trampoline jumps he has to make in order to reach shop N and see his wife again!

Input

The first line consists of one integer T, the number of test cases.

Each test case consists of two lines, the first line contains a single integer (2 ≤ N ≤ 105) the number of shops in the mall. The second line contains N integers. Where the ith integer (1 ≤ di ≤ N) is the constant described above for the ith shop.

Output

For each test case, print N lines. The ith line should contain one integer, the minimum number of jumps needed to be made in order to reach shop N starting from shop i, or  - 1 if it is impossible.

Example

Input

2
5
1 1 1 1 1
5
2 2 2 2 2

Output

4
3
2
1
0
2
-1
1
-1
0

Note

Large I/O files. Please consider using fast input/output methods.

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
vector<int>mp[N];
int vis[N];
int book[N];
void add(int u,int v)
{
    mp[v].push_back(u);
}
void bfs(int n)
{
    queue<int>q;
    q.push(n);
    book[n]=0;
    vis[n]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=0; i<(int)mp[u].size(); i++)
        {
            int v=mp[u][i];
            if(vis[v]==0)
            {
                vis[v]=1;
                q.push(v);
                book[v]=book[u]+1;
            }
        }
    }
}
int main()
{freopen("jumping.in","r",stdin);
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        memset(book,-1,sizeof(book));
        scanf("%d",&n);
        for(int i=0; i<=n; i++)
            mp[i].clear();
        for(int i=1; i<=n; i++)
        {
            int x;
            scanf("%d",&x);
            if(i+x<=n)
                add(i,i+x);
            if(i-x>=1)
                add(i,i-x);
        }
        memset(vis,0,sizeof(vis));
        bfs(n);
        for(int i=1; i<=n; i++)
            printf("%d\n",book[i]);
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值