Codeforces 408D Long Path (DP)
题目:
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 number pi, 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 pi denotes 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
题意:
如果当前到达点i的进入次数cnt为奇数 则可以到达p[i]位置 如果是偶数 可以到达i+1位置 求从1走到n+1需要的总步数 思路:
第一次走进i房间的进入次数cnt为1 是奇数 会往后退回p[i]位置 但是此刻p[i]位置的cnt也变成了奇数 因为当时只有为偶数才能向前走 那么会退回到p[p[i]]位置 不断递归
因此 dp[i]记为走到当前位置需要的总步数 状态转移方程为dp[i+1]=dp[i]+1+(dp[i]-dp[p[i]])+1 即为dp[i+1]=2*dp[i]-dp[p[i]]+2
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int maxn=1e3+;
const int mod=1e9+;
int n;
int p[maxn];
ll dp[maxn]; int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&p[i]);
}
dp[]=;
for(int i=;i<=n;i++){
dp[i+]=(*dp[i]-dp[p[i]]++mod)%mod;
}
printf("%lld\n",(dp[n+]+mod)%mod);
return ;
}
Codeforces 408D Long Path (DP)的更多相关文章
- CodeForces 407B Long Path (DP)
题目链接 题意:一共n+1个房间,一个人从1走到n+1,如果第奇数次走到房间i,会退回到房间Pi,如果偶数次走到房间i,则走到房间i+1,问走到n+1需要多少步,结果对1e9+7取模. 题解:设dp[ ...
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- Codeforces 407B Long Path(好题 DP+思维)
题目链接:http://codeforces.com/problemset/problem/407/B 题目大意:一共n+1个房间,一个人从1走到n+1,每次经过房间都会留下一个标记,每个房间有两扇门 ...
- Educational Codeforces Round 17 D. Maximum path DP
题目链接:http://codeforces.com/contest/762/problem/D 多多分析状态:这个很明了 #include<bits/stdc++.h> using na ...
- hdu-5492 Find a path(dp)
题目链接: Find a path Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU5492 Find a path[DP 方差]
Find a path Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- codeforces Hill Number 数位dp
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits: 5000 MS Memory Limits: ...
- codeforces 721C C. Journey(dp)
题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- codeforces Educational Codeforces Round 16-E(DP)
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...
随机推荐
- jenkins-ant-jmeter
jenkins下通过ant执行jmeter脚本 先下个ant 解压开来 在到jenkins中设置:系统管理-全局工具配置-ant安装-新增ant,填上name和ant-home 将jmeter的ant ...
- 剑指Offer_编程题_2
题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. class Solution ...
- 今天我们来聊聊svn的使用
前言:作为一名码农,如果你告诉你的小伙伴你不会使用版本控制,那么你将会被小伙伴所鄙视,这个文章从区别带你领略他们的优缺点. (一)git和svn之间的区别 svn相当于是一个云存储,必须要借助网络,才 ...
- 使用 highlight.js 在网页中高亮显示java 代码 【原】
<html> <head> <meta charset="UTF-8"> <script src="http://apps.bd ...
- Lost connection to MySQL server during query,MySQL设置session,global变量及网络IO与索引
Navicat导出百万级数据时,报错:2013 - Lost connection to MySQL server during query 网上一番搜索,修改mysql如下几处配置文件即可: sel ...
- UESTC - 1999 也许这是唯一能阻止乐爷AK的方法( Just for Fun )(回文树)
https://vjudge.net/problem/UESTC-1999 题意 对于一个初始为空的字符串S,你可以进行以下两种操作: 1. 在S的末尾加一个小写字母. 2. 移除S的最后一个字母. ...
- 前端常用UI框架
移动端UI框架 Mint UI(饿了么团队) 中文官网:http://mint-ui.github.io/#!/en 饿了么前端团队推出的基于Vue.js的移动端组件库 GitHub地址:https: ...
- 打印流PrintStream
打印流PrintStream PrintStream extends OutputStream 1.打印流的特点 只负责数据的输出,不负责数据的读取 与其他的流不同,打印流永远不会抛出IOExcept ...
- IIS 常见异常及解决办法
Ø 简介 IIS 是我们平常接触比较多的服务端软件,用于站点发布等,本文主要记录 IIS 常见的异常及解决办法.主要包括: 1. Visual Studio 启动 Web 项目提示"无 ...
- 微信小程序开发(5) 2048游戏
在这篇微信小程序开发教程中,我们将介绍如何使用微信小程序开发2048小游戏. 本文主要分为两个部分,小程序主体部分及小游戏页面部分 一.小程序主体部分 一个小程序主体部分由三个文件组成,必须放在项目的 ...