CodeForces 407B Long Path
来源:互联网 发布:java中的换行符 编辑:程序博客网 时间:2024/05/22 07:06
题意:
有n+1个格子 起点在1 每个格子有个前进1的门 前n个格子有个返回的门(返回前面某个格子) 奇数次走进一个格子就去走返回门 偶数次走前进门 问 走到n+1要走过几道门
思路:
一看就是DP
to[i]表示第i个格子的返回门
go[i]表示离开第i个格子需要的步数
sum[i]表示离开前i个格子需要的步数
明显 go[i]=sum[i-1]-sum[to[i]-1]+2 离开i点的步数为离开(从to[i]到i-1点)的步数和 + 第i个点返回门1步前进门1步
代码:
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<iostream>#include<string>#include<map>#include<set>#include<queue>using namespace std;#define M 1010#define mod 1000000007int go[M],sum[M],to[M];int n;int main(){ int i,j; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&to[i]); for(i=1;i<=n;i++) { go[i]=(sum[i-1]-sum[to[i]-1]+2+mod)%mod; sum[i]=(sum[i-1]+go[i])%mod; } printf("%d\n",sum[n]); return 0;}
0 0
- CodeForces 407B Long Path
- CodeForces 407B Long Path
- codeforces 407B B. Long Path(dp)
- Long Path - CodeForces 407B dp
- CodeForces 407B Long Path ( DP )
- codeforces-407B Long Path(dp)
- [杂题] Codeforces 407B #239 (Div. 1) B. Long Path
- Codeforces 407B Long Path【dp】好题
- CF 407B - Long Path
- CF - 407 - B. Long Path(dp)
- 【CODEFORCES】 B. Long Jumps
- Codeforces 408D Long Path
- Long Path (Codeforces Round #239)
- Codeforces#239_div2_D Long Path dp
- Codeforces 534B Covered Path
- codeforces-534B-Covered Path
- CodeForces 534B Covered Path
- CodeForces 534B Covered Path
- Java性能优化系列之五--JavaIO
- 代码静态检查工具汇总
- jqGrid 设置radio
- lua语言的学习(一.1)
- log4j简单配置
- CodeForces 407B Long Path
- Chrome安全架构的进化
- 聚集、非聚集索引、唯一索引、复合索引、系统自建索引
- 理解RESTful架构
- Java接口基础
- G1垃圾收集器,永久带和元数据区
- python:利用asyncio进行快速抓取
- java中的匿名内部类总结
- Android权限说明