CF1272E.Nearest Opposite Parity

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array aa consisting of nn integers. In one move, you can jump from the position ii to the position i−aii - a_i (if 1≤i−ai1 \le i - a_i) or to the position i+aii + a_i (if i+ai≤ni + a_i \le n).

For each position ii from 11 to nn you want to know the minimum the number of moves required to reach any position jj such that aja_j has the opposite parity from aia_i (i.e. if aia_i is odd then aja_j has to be even and vice versa).

给你一个包含 nn 个整数的数组 aa。在一次移动中,你可以从位置 ii 跳转到位置 i−aii - a_i(若 1≤i−ai1 \le i - a_i)或跳转到位置 i+aii + a_i(若 i+ai≤ni + a_i \le n)。

对于每个从 11 到 nn 的位置 ii,你需要求出到达任意位置 jj 所需的最少移动次数,使得 aja_j 与 aia_i 的奇偶性相反(即:若 aia_i 为奇数,则 aja_j 必须为偶数;反之亦然)。

输入格式

The first line of the input contains one integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤n1 \le a_i \le n), where aia_i is the ii-th element of aa.

输入的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 表示数组 aa 的元素个数。

输入的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤n1 \le a_i \le n),其中 aia_i 是数组 aa 的第 ii 个元素。

输出格式

Print nn integers d1,d2,…,dnd_1, d_2, \dots, d_n, where did_i is the minimum the number of moves required to reach any position jj such that aja_j has the opposite parity from aia_i (i.e. if aia_i is odd then aja_j has to be even and vice versa) or -1 if it is impossible to reach such a position.

输出 nn 个整数 d1,d2,…,dnd_1, d_2, \dots, d_n,其中 did_i 表示到达任意位置 jj 所需的最少移动次数,使得 aja_j 与 aia_i 的奇偶性相反(即:若 aia_i 为奇数,则 aja_j 必须为偶数;反之亦然);若无法到达满足条件的位置,则 di=−1d_i = -1。

输入输出样例

  • 输入#1

    10
    4 5 7 6 7 5 4 4 6 4

    输出#1

    1 1 1 2 -1 1 1 3 1 1

输入解题思路,AI测评打分。不知道怎么写?

首页