CF689B Mike and Shortcuts(单调队列/建图spfa/bfs)
来源:互联网 发布:apache ab url参数 编辑:程序博客网 时间:2024/06/06 02:59
这题做法很多,我太sb了,一直想不出来,看了题解才会。
可以只建i->i+1 i->i-1 i->a[i]三种边,然后spfa或者bfs求单源最短路。
还可以直接从左到右循环,一个点的最短距离只有两种情况,从i-1来,或者从右边某个捷径跳到的点回来,因为
#include <bits/stdc++.h>using namespace std;#define ll long long#define inf 0x3f3f3f3f#define N 200010inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}int n,ans[N],a[N];deque<int>q;int main(){// freopen("a.in","r",stdin); n=read();for(int i=1;i<=n;++i) ans[i]=i-1; for(int i=1;i<=n;++i){ a[i]=read();ans[i]=min(ans[i],ans[i-1]+1);//从左边来 while(!q.empty()&&q.front()<=i) q.pop_front(); if(!q.empty()) ans[i]=min(ans[i],ans[q.front()]+q.front()-i);//从右边来 ans[a[i]]=min(ans[a[i]],ans[i]+1); while(!q.empty()&&ans[a[i]]+a[i]<=ans[q.back()]+q.back()) q.pop_back(); q.push_back(a[i]); }for(int i=1;i<=n;++i) printf("%d ",ans[i]); return 0;}
阅读全文
0 0
- CF689B Mike and Shortcuts(单调队列/建图spfa/bfs)
- BFS-Mike and Shortcuts
- Mike and Shortcuts (CF bfs)
- CodeForces 689B Mike and Shortcuts(SPFA)
- CodeForces 689 B. Mike and Shortcuts(SPFA)
- Codeforces 689B. Mike and Shortcuts(BFS)
- 文章标题 Coderforces 689B : Mike and Shortcuts (BFS)
- codeforces 689B Mike and Shortcuts (bfs)
- CodeForces 689B BFS-Mike and Shortcuts
- Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts【BFS】
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts【BFS】
- Codeforces 689B Mike and Shortcuts DFS or BFS
- CSU-ACM2017暑期训练6-bfs Mike and Shortcuts
- B. Mike and Shortcuts(cf#361)
- Mike and Shortcuts
- Mike and Shortcuts CodeForces
- 【打CF,学算法——三星级】CodeForces 689B Mike and Shortcuts (最短路+spfa)
- Dora,this is a new day
- 图像滤波处理对比
- Java将图片压缩至指定大小
- Java异常处理
- Centos 7.3 用户和组管理
- CF689B Mike and Shortcuts(单调队列/建图spfa/bfs)
- 跟着实例学习ZooKeeper的用法: 计数器
- Less的模式匹配
- JUnit4 注解基本介绍
- 我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
- 图像锐化处理
- 三年时光或许被学校糟蹋了
- 队花的烦恼一
- Windows下用命令行导出导入MySQL数据库