Codeforces Round #605 (Div. 3) E - Nearest Opposite Parity
题目链接: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的更多相关文章
- 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 ...
- Codeforces Round #605 (Div. 3) E - Nearest Opposite Parity (超级源点)
- Codeforces Round #605 (Div. 3)
地址:http://codeforces.com/contest/1272 A. Three Friends 仔细读题能够发现|a-b| + |a-c| + |b-c| = |R-L|*2 (其中L ...
- Codeforces Round #605 (Div. 3) 题解
Three Friends Snow Walking Robot Yet Another Broken Keyboard Remove One Element Nearest Opposite Par ...
- 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 ...
- 【cf比赛记录】Codeforces Round #605 (Div. 3)
比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...
- 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 ...
- 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 ...
- Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)
链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...
随机推荐
- Redis必备面试题《基础篇》
Date:2019-11-12 读前思考: 面试官会问什么样的问题? 所问的问题背后真实的套路是什么? 喜欢问Redis哪些问题? 如何顺畅回答面试问的问题?吊打面试官. 1.什么是Redis? Re ...
- CentOS7安装PPTP
CentOS7安装PPTP VPN(开启firewall防火墙) 1 准备一个CentOS7服务器 2 检查是否支持PPTP && echo ok #返回OK ...
- [spark程序]统计人口平均年龄(本地文件)(详细过程)
一.题目描述 (1)编写Spark应用程序,该程序可以在本地文件系统中生成一个数据文件peopleage.txt,数据文件包含若干行(比如1000行,或者100万行等等)记录,每行记录只包含两列数据, ...
- 理解Spark运行模式(二)(Yarn Cluster)
上一篇说到Spark的yarn client运行模式,它与yarn cluster模式的主要区别就是前者Driver是运行在客户端,后者Driver是运行在yarn集群中.yarn client模式一 ...
- Swoole和Redis实现的并发队列处理系统
由于PHP不支持多线程,但是作为一个完善的系统,有很多操作都是需要异步完成的.为了完成这些异步操作,我们做了一个基于Redis队列任务系统. 大家知道,一个消息队列处理系统主要分为两大部分:消费者和生 ...
- 安装Fedora后
更新操作系统版本: https://fedoraproject.org/wiki/DNF_system_upgrade 靠谱: 设置ssh:ssh生成公钥私钥.默认root(.ssh/confi ...
- openresty如何完美替换nginx
下载openresty wget https://openresty.org/download/openresty-1.15.8.1.tar.gz tar zxvf openresty-1.15.8. ...
- Vue注册组件命名时不能用大写的原因浅析
命名使用注意事项: https://www.jb51.net/article/160227.htm
- convert svn repo to git
https://john.albin.net/git/convert-subversion-to-git 1. 抓取Log 在linux 上做的,其余是在win上做的. 2. svn co svn:/ ...
- python3 之 匿名函数
一.语法: lambda 参数:方法(或三元运算) #最多支持3元运算 二.实例1:基础 #函数1: a = lambda x:x*x print(a(2)) #函数2: def myfun(x): ...