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 ...
随机推荐
- PostgreSQL 预写日志机制(WAL)
关于持久性 持久性是指,事务提交后,对系统的影响必须是永久的,即使系统意外宕机,也必须确保事务提交时的修改已真正永久写入到永久存储中. 最简单的实现方法,当然是在事务提交后立即刷新事务修改后的数据到磁 ...
- Java开发笔记(一百四十六)JDBC的应用原理
关系数据库使得海量信息的管理成为现实,但各家数据库提供的编程接口不尽相同,就连SQL语法也有所差异,像Oracle.MySQL.SQL Server都拥有自己的开发规则,倘若Java针对每个数据库单独 ...
- Anaconda的pip加速下载命令
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
- hadoop2.x大数据视频教程(十二天学会)
- 【LEETCODE】65、字符分类,medium&easy级别,题目:20、647、3
今天的字符类还比较简单 package y2019.Algorithm.str.easy; import java.util.HashMap; import java.util.Map; import ...
- head first c# -- 第七章 (接口与抽象类)
接口的作用: 例子:有鸡,鸭,牛,羊4个类,还有farmer类: farmer.feed(obj obj) { obj.eat() } // 没有接口: farmer.feedChicken(Chic ...
- Redis Cluster: (error) MOVED
I have a Redis cluster with the following nodes: 192.168.0.14:6379 master (slots from 0 to 16383) ...
- highcharts离线导出图表
到了这里,其实还没有结束,导出图片时,仍会发出两个请求 此时找到offline-exporting.js文件修改其中的libURL 修改为请求自己的网站
- 2.NET Core设定数据库种子
1.使用以下代码在 Models 文件夹中创建一个名为 SeedData 的新类: using Microsoft.EntityFrameworkCore;using Microsoft.Extens ...
- java之mybatis之模糊查询
1.在 mybatis 中,模糊查询可以有以下方式 (1).第一种,直接将封装好的条件传给 sql 语句 <select id="findByName" parameterT ...