cf div2 239 D
1 second
256 megabytes
standard input
standard output
One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one.
The maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number i (1 ≤ i ≤ n), someone can use the first portal to move from it to room number (i + 1), also someone can use the second portal to move from it to room numberpi, where 1 ≤ pi ≤ i.
In order not to get lost, Vasya decided to act as follows.
- Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1.
- Let's assume that Vasya is in room i and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room pi), otherwise Vasya uses the first portal.
Help Vasya determine the number of times he needs to use portals to get to room (n + 1) in the end.
The first line contains integer n (1 ≤ n ≤ 103) — the number of rooms. The second line contains n integers pi (1 ≤ pi ≤ i). Each pidenotes the number of the room, that someone can reach, if he will use the second portal in the i-th room.
Print a single number — the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007 (109 + 7).
2
1 2
4
4
1 1 2 3
20
5
1 1 1 1 1
62 貌似用了挺复杂的dp ,应该是写丑了
把每个点分成两个状态,一个奇数一个偶数,边往下走边保存第一次遇到某个状态的步数,当下一个状态是前面出现过的状态可以直接计算再一次回到目前的状态,否则往下一个状态走。妥妥写丑了... 刚刚突然发现原来我忽略了 pi <= i ,好吧,我自己把题目变难了,我貌似解决了这样的问题Can you solve this task without statement pi ≤ i? I don't know the solution, it seems difficult.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long ll; const int MAX_N = 1e3 + ;
const int MOD = 1e9 + ;
int N;
int p[MAX_N],sta[MAX_N],dp[ * MAX_N]; int cal(int x) {
return (!sta[x]) * (N + ) + x;
} int main()
{
scanf("%d",&N);
fill(dp + ,dp + * (N + ) + ,-);
for(int i = ; i <= N; ++i) {
scanf("%d",&p[i]);
} sta[] = ;
int now = ,ans = ,next;
while(now != N + ) {
if(dp[cal(now)] == -)
dp[ cal(now)] = ans;
if(sta[now] % ) {
next = p[now];
} else {
next = now + ;
} int nowid = cal(now),nextid;
sta[next] = !sta[next];
nextid = cal(next);
if( dp[ cal(next) ] != -) {
ans = ( (ll)ans + (dp[nowid] - dp[nextid]) + MOD + ) % MOD;
sta[now] = (sta[now] + ) % ;
sta[next] = !sta[next];
} else {
now = next;
ans = (ans + ) % MOD;
}
} printf("%d\n",ans);
return ;
}
cf div2 239 D的更多相关文章
- cf div2 234 D
D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 离线dfs CF div2 707 D
http://codeforces.com/contest/707/problem/D 先说一下离线和在线:在线的意思就是每一个询问单独处理复杂度O(多少多少),离线是指将所有的可能的询问先一次都处理 ...
- cf div2 236 D
D. Upgrading Array time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- cf div2 237 D
D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...
- cf div2 238 D
D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- cf div2 238 c
C. Unusual Product time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- cf div2 235 D
D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...
- cf div2 234 E
E. Inna and Binary Logic time limit per test 3 seconds memory limit per test 256 megabytes input sta ...
- CF div2 D BFS
http://codeforces.com/contest/676/problem/D 题目大意: 勇者去迷宫杀恶龙.迷宫是有n*m的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...
随机推荐
- RMAN 报:ORA-19504 ORA-27038
在itpub中看到下面的问题: oracle 10g备份脚本如下run{allocate channel d1 device type disk MAXPIECESIZE=100M;crosschec ...
- CSS3中新出现的技术
CSS3中新出现的技术 CSS媒体查询 媒体查询 包含了一个媒体类型和至少一个使用如宽度.高度和颜色等媒体属性来限制样式表范围的表达式.CSS3加入的媒体查询使得无需修改内容便可以使样式应用于某些特定 ...
- 一步一步实现Linux设备驱动的Helloworld模块
学了那么多程序语言,总是有一个Hello world开头,不禁感叹Hello world的强大.呵呵,废话少说,咋们的故事当然要从这个Hello world开始. 先查看自己OS使用的内核版本[don ...
- Android的快速开发框架afinal
afinal 是一个android的 orm 和 ioc 框架.而且封装了android中的httpClient,使其更加简单易用. afinal是android应用开发的终极框架. FinalDB使 ...
- 禁用DropDownList的Items
可以先看看效果,被禁用的DropDownList的Items已经无法选取与颜色也变为灰色. 刚开始,DropDownList没有接收从网址传来的参数时,它每一个Item都是可以选取的,一旦接收了值.所 ...
- 1.总结---tr()和QTextCodec对象
1. 关于Qt 中的tr()函数-------http://tscsh.blog.163.com/blog/static/200320103201310213312518/ 在论坛中漂,经常遇到有人遇 ...
- QT 十六进制字符串转化为十六进制编码
/*************************************************Function: hexStringtoByteArray()Description: 十六进制字 ...
- angular2 国际化实现
angular2国际化通过管道(pipe)的形式实现下载ng2-translate 如何使用可以参照https://github.com/ocombe/ng2-translate 自己写了一个小DEM ...
- 代码实现Autolayout
代码实现Autolayout的步骤 利用NSLayoutConstraint类创建具体的约束对象 添加约束对象到相应的view上 - (void)addConstraint:(NSLayoutCons ...
- [转载]EF Code First 学习笔记:约定配置
要更改EF中的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面;还有一个就 ...