D. Long Path
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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).

Sample test(s)
input
2
1 2
output
4
input
4
1 1 2 3
output
20
input
5
1 1 1 1 1
output
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的更多相关文章

  1. cf div2 234 D

    D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. 离线dfs CF div2 707 D

    http://codeforces.com/contest/707/problem/D 先说一下离线和在线:在线的意思就是每一个询问单独处理复杂度O(多少多少),离线是指将所有的可能的询问先一次都处理 ...

  3. cf div2 236 D

    D. Upgrading Array time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. cf div2 237 D

    D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...

  5. cf div2 238 D

    D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  6. cf div2 238 c

    C. Unusual Product time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. cf div2 235 D

    D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...

  8. cf div2 234 E

    E. Inna and Binary Logic time limit per test 3 seconds memory limit per test 256 megabytes input sta ...

  9. CF div2 D BFS

    http://codeforces.com/contest/676/problem/D 题目大意: 勇者去迷宫杀恶龙.迷宫是有n*m的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...

随机推荐

  1. WPF实现渐变淡入淡出的动画效果

    1.实现原理 1.1 利用UIElement.OpacityMask属性,用于改变对象区域的不透明度的画笔.可以使元素的特定区域透明或部分透明,从而实现比较新颖的效果. 1.2 OpacityMask ...

  2. hdu 1029

    #include"stdio.h" int main(void) { int n,x,y,t,i; while(scanf("%d",&n)!=EOF) ...

  3. JavaScript高级程序设计之Date类型

    ECMAScript 中的 Date 类型是在早期 Java 的 java.util.Date 类基础上构建的. Date 类型使用自 UTC (国际协调时间)1970年1月1日午夜(零时)开始经过的 ...

  4. andirod

    于adnroid老手来说,SDK环境搭建是很简单的,但是对于我这样的小白来说,,,,,走啦很多弯路..特记下.希望对兄弟们有所帮助 因为我也是参考网上的高手知道,所以有的就直接复制啦,,^_^ 想要开 ...

  5. 为SM30视图分配事务代码

    Tcode:SE93

  6. P1230: [Usaco2008 Nov]lites 开关灯

    嗯嗯,这是一道线段树的题,询问区间内亮着的灯的个数,我们可以把区间修改的线段树改一下,原本的求和改成若有奇数次更改则取反(总长度-亮着的灯个数),而判断是否奇数次只要数组加一个delta的值,upda ...

  7. 立即执行函数(IIFE)的理解与运用

    作为JavaScript的常用语法,立即执行函数IIFE(Immediately-Invoked Function Expression)是值得我们认真去学习探究的. 一.创建函数的两种方式 我们先从 ...

  8. JavaScript插入节点

    1. document.write("<p>This is inserted.</p>"); 该方法必须加在HTML文档内,违背了结构行为分离原则,不推荐. ...

  9. 宣讲ppt的技巧

    这是一个L运营商的项目,项目规模比较大,中兴的客户群体定位主要是电信运营商,运营商的项目做起来非常累,不是一般的小公司能玩的,一般项目要经过这几个过程,前期信息获得——技术交流引导——实验局测试——投 ...

  10. nodeJs爬虫获取数据

    var http=require('http'); var cheerio=require('cheerio');//页面获取到的数据模块 var url='http://www.jcpeixun.c ...