Description:
You are given an array a consisting of n integers. In one move, you can jump from the position i i i to the position i − a i i−a_{i} i−ai (if 1 ≤ i − a i ) 1≤i−a_{i}) 1≤i−ai) or to the position i + a i i+a_{i} i+ai (if i + a i ≤ n ) . i+a_{i}≤n). i+ai≤n).
For each position i i i from 1 1 1 to n n n you want to know the minimum the number of moves required to reach any position j j j such that a j a_{j} aj has the opposite parity from a i a_{i} ai ( i . e i.e i.e. if a i a_{i} ai is odd then aj has to be even and vice versa).
Input
The first line of the input contains one integer n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) n (1≤n≤2⋅10^5) n(1≤n≤2⋅105) — the number of elements in a a a.
The second line of the input contains n integers a 1 , a 2 , … , a n ( 1 ≤ a i ≤ n ) a_{1},a_{2},…,a_{n} (1≤a_{i}≤n) a1,a2,…,an(1≤ai≤n), where ai is the i − t h i-th i−th element of a a a.
Output
Print n n n integers d 1 , d 2 , … , d n d_{1},d_{2},…,d_{n} d1,d2,…,dn, where d i d_{i} di is the minimum the number of moves required to reach any position j j j such that a j a_{j} aj has the opposite parity from a i a_{i} ai ( i . e i.e i.e. if ai is odd then aj has to be even and vice versa) or − 1 -1 −1 if it is impossible to reach such a position.
Example
input
10
4 5 7 6 7 5 4 4 6 4
output
1 1 1 2 -1 1 1 3 1 1
题意: 一个长度为 n 的序列,每个位置可以跳到 i − a i i−a_{i} i−ai或者 i + a i i+a_{i} i+ai,求出每个位置最少需要跳几次,可以使起始位置和结束位置的奇偶性不同。
反向建图,第一遍找可以一步到达符合要求的点,和能走到这个点的点,然后对能达到符合要求的点进行扩展。
AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n, m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sc(n) scanf("%c", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define ss(str) scanf("%s", str)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
int ret = 0, sgn = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
ret = ret * 10 + ch - '0';
ch = getchar();
}
return ret * sgn;
}
inline void Out(int a)
{
if (a > 9)
Out(a / 10);
putchar(a % 10 + '0');
}
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
return a * b / gcd(a, b);
}
///快速幂m^k%mod
ll qpow(int m, int k, int mod)
{
ll res = 1, t = m;
while (k)
{
if (k & 1)
res = res * t % mod;
t = t * t % mod;
k >>= 1;
}
return res;
}
// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
return qpow(a, p - 2, p);
}
///扩展欧几里得
int exgcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int g = exgcd(b, a % b, x, y);
int t = x;
x = y;
y = t - a / b * y;
return g;
}
///使用ecgcd求a的逆元x
int mod_reverse(int a, int p)
{
int d, x, y;
d = exgcd(a, p, x, y);
if (d == 1)
return (x % p + p) % p;
else
return -1;
}
///中国剩余定理模板
ll china(int a[], int b[], int n) //a[]为除数,b[]为余数
{
int M = 1, y, x = 0;
for (int i = 0; i < n; ++i) //算出它们累乘的结果
M *= a[i];
for (int i = 0; i < n; ++i)
{
int w = M / a[i];
int tx = 0;
int t = exgcd(w, a[i], tx, y); //计算逆元
x = (x + w * (b[i] / t) * x) % M;
}
return (x + M) % M;
}
const int N = 2e5 + 10;
int n;
int a[N];
vector<int> v[N];
queue<int> q;
bool vis[N];
int dis[N];
int main()
{
sd(n);
rep(i, 1, n)
sd(a[i]);
rep(i, 1, n)
dis[i] = inf;
mem(vis, 0);
rep(i, 1, n)
{
if (i - a[i] > 0)
v[i - a[i]].pb(i);
if (i + a[i] <= n)
v[i + a[i]].pb(i);
if ((i - a[i] > 0 && a[i - a[i]] % 2 != a[i] % 2) || (i + a[i] <= n && a[i + a[i]] % 2 != a[i] % 2))
{
dis[i] = 1;
q.push(i);
vis[i] = 1;
}
} //反向建图
while (q.size())
{
int now = q.front();
q.pop();
int len = v[now].size();
rep(i, 0, len - 1)
{
int res = v[now][i];
if (a[res] % 2 == a[now] % 2 && !vis[res])
{
q.push(res);
vis[res] = 1;
dis[res] = dis[now] + 1;
}
}
}
rep(i, 1, n)
{
if (vis[i])
printf("%d ", dis[i]);
else
printf("-1 ");
}
return 0;
}