题目链接:http://codeforces.com/contest/1272/problem/E

题意:给定n,给定n个数a[i],对每个数输出d[i]。

对于每个i,可以移动到i+a[i]和i-a[i](如果i+a[i]<=n,i-a[i]>=1)

d[i]是指从i移动到任意一个j的步数,需满足条件a[i]和a[j]的奇偶性不同

不论奇偶,相连的边先放进vector邻接表中

如果i和i+a[i]奇偶性不同,那么ans[i]为1,把i放到queue队列里

同理,如果i和i-a[i]奇偶性不同,那么ans[i]为1,把i放到queue队列里

(bfs)

queue队列里存的是每个有答案的点,刚开始队列里所有点的ans都为1。

由于需要a[i]和a[j]奇偶性不同,则只需要跟有答案的点奇偶性相同即可。

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+;
vector<int> v[maxn];
int ans[maxn],a[maxn];
int main()
{
memset(ans,-,sizeof ans);
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)cin>>a[i]; queue<int> q;
for(int i=;i<=n;i++)
{
int j=i+a[i];
if(j<=n)
{
v[j].push_back(i);
if(a[j]%!=a[i]%)
{
ans[i]=;
q.push(i);
}
}
j=i-a[i];
if(j>=)
{
v[j].push_back(i);
if(a[j]%!=a[i]%)
{
ans[i]=;
q.push(i);
}
}
}
while(!q.empty())//bfs
{
int cur=q.front();
q.pop();
for(int n:v[cur])
{
if(ans[n]==-&&a[n]%==a[cur]%)
{
ans[n]=ans[cur]+;
q.push(n);
}
}
}
for(int i=;i<=n;i++)
{
cout<<ans[i]<<" ";
} return ;
}

Codeforces Round #605 (Div. 3) E - Nearest Opposite Parity的更多相关文章

  1. Codeforces Round #605 (Div. 3) E. Nearest Opposite Parity(最短路)

    链接: https://codeforces.com/contest/1272/problem/E 题意: You are given an array a consisting of n integ ...

  2. Codeforces Round #605 (Div. 3) E - Nearest Opposite Parity (超级源点)

  3. Codeforces Round #605 (Div. 3)

    地址:http://codeforces.com/contest/1272 A. Three Friends 仔细读题能够发现|a-b| + |a-c| + |b-c| = |R-L|*2 (其中L ...

  4. Codeforces Round #605 (Div. 3) 题解

    Three Friends Snow Walking Robot Yet Another Broken Keyboard Remove One Element Nearest Opposite Par ...

  5. Codeforces Round #172 (Div. 2) B. Nearest Fraction 二分

    B. Nearest Fraction Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/281/p ...

  6. 【cf比赛记录】Codeforces Round #605 (Div. 3)

    比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...

  7. Codeforces Round #605 (Div. 3) D. Remove One Element(DP)

    链接: https://codeforces.com/contest/1272/problem/D 题意: You are given an array a consisting of n integ ...

  8. Codeforces Round #605 (Div. 3) C. Yet Another Broken Keyboard

    链接: https://codeforces.com/contest/1272/problem/C 题意: Recently, Norge found a string s=s1s2-sn consi ...

  9. Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)

    链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...

随机推荐

  1. python机器学习——逻辑回归

    我们知道感知器算法对于不能完全线性分割的数据是无能为力的,在这一篇将会介绍另一种非常有效的二分类模型--逻辑回归.在分类任务中,它被广泛使用 逻辑回归是一个分类模型,在实现之前我们先介绍几个概念: 几 ...

  2. MyBatis两种传参方式的区别

    $与#的区别 select * from T_PRINT_LAYOUT where D_RECID = ${recId} 最后生成的SQL为: select * from T_PRINT_LAYOUT ...

  3. secureCRT连接虚拟机

    1.secureCRT英文版下载 链接:https://pan.baidu.com/s/1LFWD-k2r4ZB7DHQA66QogQ 密码:khmo 破解方式参考 2.虚拟机静态IP设置 参考 3. ...

  4. 手动部署LNMP环境(CentOS 7)

    手动部署LNMP环境(CentOS 7) 一.修改 yum 源 [root@localhost ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/e ...

  5. nyoj 26-孪生素数问题(打表)

    26-孪生素数问题 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:10 submit:43 题目描述: 写一个程序,找出给出素数范围内的所有孪生素数 ...

  6. Bootstrap——导航条(navbar)

    导航条和导航从外观上差别不是太多,但在实际使用中导航条要比导航复杂得多. 导航条(navbar)中有一个背景色.而且导航条可以是纯链接(类似导航).表单以及表单和导航一起结合等多种形式. 在制作一个基 ...

  7. ASP使用ajax来传递中文参数的编码处理

    背景 asp的第一版是0.9测试版,自从1996年ASP1.0诞生,迄今20余载.虽然asp在Windows2000 IIS服务5.0所附带的ASP 3.0发布后好像再没有更新过了,但是由于其入手简单 ...

  8. python爬取中国知网部分论文信息

    爬取指定主题的论文,并以相关度排序. #!/usr/bin/python3 # -*- coding: utf-8 -*- import requests import linecache impor ...

  9. Siamese-RPN论文阅读

    https://www.cnblogs.com/zhengyuqian/p/10609737.html

  10. CSS中如果实现元素浮动和清除浮动,看这篇文章就足够了

    浮动基本介绍 在标准文档流中元素分为2种,块级元素和行内元素,如果想让一些元素既要有块级元素的特点也同时保留行内元素特点,只能让这些元素脱离标准文档流即可. 浮动可以让元素脱离标准文档流,可以实现让多 ...