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的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...
随机推荐
- hdu 5264 pog loves szh I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5264 pog loves szh I Description Pog has lots of stri ...
- JavaScript高级程序设计之EventUtil
简单的通用事件方法 var EventUtil = { getEvent: function (e) { return e || window.event; }, getTarget: functio ...
- iOS学习之事件处理
一.事件的基本概念 1.事件概述 事件是当用户手指触击屏幕及在屏幕上移动时,系统不断发送给应用程序的对象. 系统将事件按照特定的路径传递给可以对其进行处理的对象. 在iOS汇总,一个UITo ...
- 浅析Objective-C字面量
编写Objective-C程序时,总会用到某几个类,它们属于Foundation框架.虽然从技术上来说,不用Foundation框架也能写出Objective-C代码,但实际上却经常要用到此框架.这几 ...
- OC中NSDictionary(字典)、NSMutableDictionary(可变字典)、NSSet(集合)、NSMutableSet(可变集合)得常用方法
字典用于保存具有映射关系数据的集合 一个key—value对认为是一个条目(entry),字典是存储key—value对的容器 与数组不同,字典靠key存取元素 key不能重复,value必须是对象 ...
- having count group by
select count(*) from (select field2,count(field2) from bsgj.table1 group by field,items_id having(c ...
- C#语法功能结构
1.File打开指定文件夹或者文件,"\"为转义字符System.Diagnostics.Process.Start(Application.StartupPath + " ...
- iOS 进阶 第十五天(0417)
0417 创建UICollectionViewCell的Xib方法如下图 枚举定义导航跳转方式 js跳转到网页指定锚点 如下图所示:
- 在使用SQLite插入数据时出现乱码的解决办法
在VC++中通过sqlite3.dll接口对sqlite数据库进行操作,包括打开数据库,插入,查询数据库等,如果操作接口输入参数包含中文字符,会导致操作异常.例如调用sqlite3_open打开数 ...
- jQuery中ready与load事件
jQuery中ready与load事件(来自慕课网) jQuery有3种针对文档加载的方法 $(document).ready(function() { // ...代码... }) //docume ...