题目链接: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. PHP curl下载图片的方法

    PHP curl下载图片的方法 <pre> <?php $images = [ 'http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKPkia3rx ...

  2. Jetpack Compse 实战 —— 全新的开发体验

    公众号回复 Compose 获取安装包 项目地址: Wanandroid-Compose 经过前段时间的 Android Dev Summit ,相信你已经大概了解了 Jetpack Compose ...

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

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

  4. pat 1027 Colors in Mars(20 分)

    1027 Colors in Mars(20 分) People in Mars represent the colors in their computers in a similar way as ...

  5. LF模式解决的问题

    一说起Leader/Followers并发模式,都会与Half-Async/Half-Sync并发模式进行比较,说LF模式更加高性能,成了一个高性能名词标签 符号,相反HA/HS仿佛成了一个低性能的名 ...

  6. [FPGA]浅谈LCD1602字符型液晶显示器(Verilog)

    目录 概述 LCD1602 LCD1602是什么? LCD1602的管脚 RS_数据/命令选择 E_使能 D0-D7 LCD1602有个DDRAM LCD1602还有个CGROM 指令集 清屏 进入模 ...

  7. 2019-9-11:渗透测试,Kill远控软件,初接触

     初步使用Kill远控软件,使win7靶机被远控 该文章仅供学习,利用方法来自网络文章,仅供参考   1,打开运行Kill,选择系统设置,设置监听端口,通讯密码,点击保存设置 2,点击服务生成,上线参 ...

  8. Protues7.8仿真软件有中文路径无法正常运行怎么办?

    Protues7.8是一款功能强大的单片机仿真软件,在我们的学习生活中经常会用的到,在装软件时明明已经装好了,却不能报错跳出两行红字,让人心痛. 一般都是因为账户名字是中文的问题,这个软件对中文不兼容 ...

  9. 用Spring Security, JWT, Vue实现一个前后端分离无状态认证Demo

    简介 完整代码 https://github.com/PuZhiweizuishuai/SpringSecurity-JWT-Vue-Deom 运行展示 后端 主要展示 Spring Security ...

  10. RestSharp Simple REST and HTTP API Client for .NET

    var client = new RestClient("http://example.com"); // client.Authenticator = new HttpBasic ...