CF605E Nearest Opposite Parity【最短路】【多源变单源】【超级源点】

Nearest Opposite Parity

You are given an array 𝑎 consisting of 𝑛 integers. In one move, you can jump from the position 𝑖 to the position 𝑖−𝑎𝑖 (if 1≤𝑖−𝑎𝑖) or to the position 𝑖+𝑎𝑖 (if 𝑖+𝑎𝑖≤𝑛).

For each position 𝑖 from 1 to 𝑛 you want to know the minimum the number of moves required to reach any position 𝑗 such that 𝑎𝑗 has the opposite parity from 𝑎𝑖 (i.e. if 𝑎𝑖 is odd then 𝑎𝑗 has to be even and vice versa).

Input
The first line of the input contains one integer 𝑛 (1≤𝑛≤2e5) — the number of elements in 𝑎.

The second line of the input contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤𝑛), where 𝑎𝑖 is the 𝑖-th element of 𝑎.

Output
Print 𝑛 integers 𝑑1,𝑑2,…,𝑑𝑛, where 𝑑𝑖 is the minimum the number of moves required to reach any position 𝑗 such that 𝑎𝑗 has the opposite parity from 𝑎𝑖 (i.e. if 𝑎𝑖 is odd then 𝑎𝑗 has to be even and vice versa) or -1 if it is impossible to reach such a position.

Example
input

10
4 5 7 6 7 5 4 4 6 4

output

1 1 1 2 -1 1 1 3 1 1 

题意

一个数组,i位置可以到达i +/- a[ i ] 位置(不越界情况下),问你每个位置要走到一个奇偶性相反的地点最少要走几次

分析 【超级源点】【反向思维】

此题是一个比较典型的多源变单源的最短路问题,以奇数为例,要求得某个奇数可以由哪些偶数走过来,就应该以所有的偶数为起点出发,看谁能先到达此奇数点,在使用Dijkstra时,可以把所有的偶数点都放进去,进行扩展。也可以用一个超级源点来连接所有偶数点,在使用Dijkstra时,把此超级源点放进去就行了。然后就跑单源最短路算法,因为每一个奇数点都是由偶数点为起点得出的路径,此时就可以反向思维,是偶数点走到奇数点,且这条路径是最短的 这里我用的是堆优化版的Dijkstra算法。

#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const int maxn = 1e6+10;
typedef long long ll;
typedef pair<int,int> pii;

int N;
int arr[maxn];
int h[maxn],e[maxn],ne[maxn],idx;
int vis[maxn],dis[maxn],res[maxn];
void add(int a,int b){
    e[idx] = b,ne[idx] = h[a],h[a] = idx++;
}

void dij(int s){
    priority_queue<pii,vector<pii>,greater<pii> > q;
    memset(vis,0,sizeof(vis));
    memset(dis,0x3f,sizeof(dis)); dis[s] = 0;
    q.push({0,s});
    while(!q.empty()){
        pii cur = q.top();q.pop();
        int d = cur.first,u = cur.second;
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = h[u];i!=-1;i = ne[i]){
            int v = e[i];
            if(d+1<dis[v]){
                dis[v] = d+1;
                q.push({dis[v],v});
            }
        }
    }
}

int main(){
    memset(h,-1,sizeof(h));
    cin>>N;
    for(int i = 0;i<N;i++) scanf("%d",&arr[i]);
    for(int i = 0;i<N;i++){
        if(i-arr[i] >= 0) add(i-arr[i],i);
        if(i+arr[i] < N) add(i+arr[i],i);
        if(arr[i]%2 == 0) add(N+0,i);//N+0 为所有偶数点的超级源点
        if(arr[i]%2 == 1) add(N+1,i);//N+1 为所有奇数点的超级源点
    }
    dij(N+0);
    for(int i = 0;i<N;i++) if(arr[i]%2 == 1) res[i] = dis[i]; //得出来的奇->偶路径复制到结果中
    dij(N+1);
    for(int i = 0;i<N;i++) if(arr[i]%2 == 0) res[i] = dis[i]; //得出来的偶->奇路径复制到结果中
    for(int i = 0;i<N;i++){
        if(res[i]>=0x3f3f3f3f) printf("-1 ");
        else printf("%d ",res[i]-1);//因为加入了超级源点,所以距离-1
    }
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
05-20 16:14
已编辑
不止遇到一次了,什么都不会,让提合并请求,问什么是合并请求。让gitlab.页面把测试截图附上,不知道截图要放在哪,那么大的编辑看不到吗让配开发机,问ip是什么东西……这都咋进来的啊,我们(我2023年毕业)那会儿没AI的时候面试都是直接linux,docker,k8s,git,结构与算法,计网。怎么才过去2年,实习生跟傻子一样,有些问题问的我难受,不会git&nbsp;commit,不会git&nbsp;pull,不会切换分支,直接要覆盖master....————而且态度非常敷衍,3天前给开个仓库权限,连本地都没有拉下来。让写一个小文档,都是说一句,写一句,说把目录加上,挺嗤之以鼻,最后还是把目录加上了😂😂任何文档和注释都是方便后来人的,现在的人真的很自负啊,打开github看看任何一个开源项目的文档和注释,都写的很详细。难道现在的同学在校期间不经常拉开源项目看源码学习吗?&nbsp;哪怕是一个swap函数,开源项目里都经常注释:1&nbsp;3&nbsp;5&nbsp;7&nbsp;9&nbsp;2&nbsp;4&nbsp;6&nbsp;8&nbsp;10^&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^l&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rswap:{功能描述}{使用样例}————给我气笑了,没次问我有什么任务的时候,我都是说,优先你学校导师的项目,然后再做公司需求。然后给了两个需求,一个月内搞定就行,既然是agent开发,1.&nbsp;部署需要维护项目的开发环境2.阅读opencode/openclaude代码(我个人感觉龙虾的源码agent部分很常规,就一个channel+agent,还不如看claude泄露的代码和opencode)然后任务1搞了几周说因为环境问题,他申请到的远程开发机是linux,装的python2,项目是py3的,所以没搭建,我说你不行就用conda或docker把环境屏蔽了呢,没搭理我。任务2:看了很长时间代码,给我回了一句,opencode和openclaude是用go写的……我说你打开github看右下角那的语言是ts还是go……&nbsp;结果满脸懵的说ts是什么……我让看agent&nbsp;loop,哪怕全局搜索一下while(true),跳过去从头看到尾就大致清楚了,压根没看。————嘻嘻,我已经开始做社招简历了。
redf1sh:默认会git结果发现真不会,这种一看就是没做过项目的,真做过项目的至少会提交
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务