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 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).
思路:
写了好久DFS发现有环不能处理。。看了大佬博客才懂。
考虑从a开始的最短路,计算的是a到x的最短路。
反向建图,跑出来的最短路,就是他能到达的点往自己的最短路。
建立两个新点,分别连奇数点和偶数点,跑最短路。
代码:
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MAXN = 2e5+10;
vector<int> G[MAXN];
int a[MAXN], ans[MAXN], dis[MAXN], vis[MAXN];
int n;
void SPFA(int s)
{
queue<int> que;
for (int i = 1;i <= n+2;i++)
dis[i] = INF, vis[i] = 0;
que.push(s);
dis[s] = 0;
vis[s] = 1;
while(!que.empty())
{
int u = que.front();
que.pop();
vis[u] = 0;
for (int i = 0;i < (int)G[u].size();++i)
{
int node = G[u][i];
if (dis[node] > dis[u]+1)
{
dis[node] = dis[u]+1;
if (vis[node] == 0)
{
que.push(node);
vis[node] = 1;
}
}
}
}
}
int main()
{
cin >> n;
for (int i = 1;i <= n;++i)
cin >> a[i];
for (int i = 1;i <= n;++i)
{
if (i-a[i] >= 1)
G[i-a[i]].push_back(i);
if (i+a[i] <= n)
G[i+a[i]].push_back(i);
}
for (int i = 1;i <= n;++i)
{
if (a[i]%2 == 1)
G[n+1].push_back(i);
else
G[n+2].push_back(i);
}
SPFA(n+1);
for (int i = 1;i <= n;++i)
if (a[i]%2 == 0) ans[i] = dis[i];
SPFA(n+2);
for (int i = 1;i <= n;++i)
if (a[i]%2 == 1) ans[i] = dis[i];
for (int i = 1;i <= n;++i)
cout << ((ans[i] == INF) ? -1 : ans[i]-1) << ' ' ;
cout << endl;
return 0;
}
Codeforces Round #605 (Div. 3) E. Nearest Opposite Parity(最短路)的更多相关文章
- 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 ...
- 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 ...
随机推荐
- java实现限流
问题产生,当调用一个接口很频繁的时候,比如每秒调用一个接口100次.业务提现在抢购等.这时我们的服务器处理不过来就会拒绝服务,宕机等等...显然这不是我们需要的. 因此产生了限流这个.限流是什么呢,就 ...
- Java学习笔记二——正则表达式
Java正则表达式 正则表达式的规则 “abc” 匹配字符串abc [abc] 匹配[]里任意一个字符 [a-z]: 匹配所有小写字母中的任意一个字符 [A-Z]: 匹配所有大写字母中的任意一个字符 ...
- 2019-7-18 collections,time,random,os,sys,序列化模块(json和pickle)应用
一.collections模块 1.具名元组:namedtuple(生成可以使用名字来访问元素的tuple) 表示坐标点x为1 y为2的坐标 注意:第二个参数可以传可迭代对象,也可以传字符串,但是字 ...
- 国内P2P网贷行业再次大清理,仅剩646家
最近有网贷行业头部网站流出消息,国内网贷行业再次迎来大洗牌 清扫之后网贷的平台数量仅剩646家,数量陡降 根据小编了解.自2007年国外网络借贷平台模式引入中国以来,由于国家一时没有做出相应规定个条例 ...
- pytest_06_fixture之yield实现teardown
上一篇讲到fixture通过scope参数控制setup级别,既然有setup作为用例之前前的操作,用例执行完之后那肯定也有teardown操作. 这里用到fixture的teardown操作并不是独 ...
- [cf 585 E] Marbles
(一道Div2E不会,我太难了) 题意: 给你一个长度为$n$的颜色序列$A$,每次操作可以选择两个相邻元素交换,求把序列交换成“相同颜色挨在一起”所需的最少操作数. 按颜色排序:设颜色$col$在序 ...
- npm和Node.js简介
Node.js Node.js是JavaScript的一种运行环境,是对Google V8引擎进行的封装.是一个服务器端的javascript的解释器.Node.js允许通过JavaScript和一系 ...
- MySQL中的JSON类型
前言(废话) 昨天抽了点时间在网上搜列了一个开源项目,项目挺完整的,前后台分离还带有微信小程序,我Clone下代码,经过一番倒腾,嘿~还真就跑起来了.在这个过程中,体验了一把VUE项目工程细节,因为之 ...
- js基础闭包练习题
题目描述 实现函数 makeClosures,调用之后满足如下条件:1.返回一个函数数组 result,长度与 arr 相同2.运行 result 中第 i 个函数,即 result[i](),结果与 ...
- Linux目录结构说明
文件系统层级标准(filesystem hierarchy standard,FHS). http://www.pathname.com/fhs/pub/fhs-2.3.html 以下是对这些目录的解 ...