E. Nearest Opposite Parity

这是一道关于最短路径的问题,题目要求在每次从位置i跳到i-ai或i+ai的位置后,找到最少的步数到达一个具有相反奇偶性的元素。输入包含一个整数n和数组a,输出每个位置i到具有相反奇偶性的位置j所需的最小步数或-1表示不可能到达。
摘要由CSDN通过智能技术生成

传送口

之前学长让我们练习最短路径时我摸鱼了,刚刚补了spf那部分知识点,训练就遇到类似的题,但是非常不好想,以后不摸鱼了。
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i−ai (if 1≤i−ai) or to the position i+ai (if i+ai≤n).

For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that aj has the opposite parity from ai (i.e. if 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⋅105) — the number of elements in a.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤n), where ai is the i-th element of a.

Output
Print n integers d1,d2,…,dn, where di is the minimum the number of moves required to reach any position j such that aj has the opposite parity from ai (i.e. if ai is odd then aj has to be even and vice versa) or -1 if it is impossible to reach such a position.

#include<iostream>
#include<cstring>
#include<queue>
#include<deque>
#include<vector>
using namespace std;
const int maxn=2e6+5;
int a[maxn],dis[maxn],vis[maxn],ans[maxn];
vector<int>son[maxn];
void spf(int u)
{
   
    deque<int>in;
    memset(dis,0x3f3f3f3f,sizeof(dis));
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值