E - Jumping

jumping.in / standard output

​Statements​

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

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

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 Nintegers. Where the ith integer (1 ≤ di ≤ N) is the constant described above for the ith

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

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.

题意:x轴上有很多商店,每个商店有一个蹦床可以让你前进di步或者后退di步,问从每个商店为起点,到达第N个商店的最少步数

每个点到终点求最短路,反过来想就是终点往前做一次最短路,直接递归模拟按数据来说是应该会超时的(10^5),所以在读取点的时候构建一张关系邻接表,然后以N为起点做一次最短路就可以了。

【代码】

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
int f[100100];
vector<int> a[100100];
int vis[100100];
int n,pp,x;
void bfs()
{
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(n);
vis[n]=1;
f[n]=0;
while (!q.empty())
{
x=q.front();
q.pop();
for (int i=0;i<a[x].size();i++)
if (!vis[a[x][i]])
{
q.push(a[x][i]);
f[a[x][i]]=f[x]+1;
vis[a[x][i]]=1;
}
else
f[a[x][i]]=min(f[a[x][i]],f[x]+1);
}
}

int main()
{
FILE *p1;
p1=fopen("jumping.in","r");
fscanf(p1,"%d",&pp);
while (pp--)
{
memset(f,-1,sizeof(f));
for (int i=1;i<=n;i++) a[i].clear();
fscanf(p1,"%d",&n);
for (int i=1;i<=n;i++)
{
fscanf(p1,"%d",&x);
if (i-x>0) a[i-x].push_back(i);
if (i+x<=n) a[i+x].push_back(i);
}
bfs();
for (int i=1;i<=n;i++)
printf("%d\n",f[i]);
}
}