zoukankan      html  css  js  c++  java
  • CF 407B Long Path[观察性质 DP]

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

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

    Examples
    input
    2
    1 2
    output
    4
    input
    4
    1 1 2 3
    output
    20
    input
    5
    1 1 1 1 1
    output
    62

    官方题解
    In this problem you had to simulate route of character in graph. 
    Note that if you are in vertice i, then edges in all vertices with numbers less than i are turned to pi. It gives us opportunity to see a recurrence formula: let dpi be number of steps, needed to get from vertice 1 to vertice i, if all edges are rotated back, into pi. Then dpi + 1 = 2dpi + 2 - dppi. Answer will be dpn + 1.

    很明显要用到DP

    考虑d+1,只可能从i走到,但是第一次到达i,下一步会走到pi,然后又到达i

    可以发现到i时i之前的全是偶数个叉,所以这两次的传送门数是一样的,第二次只是少了到p[i]到次数而已

    d[i]表示第一次到达i的传送门次数,d[i]=d[i]+1+(d[i]-d[p[i]])+1

    //
    //  main.cpp
    //  cf407b
    //
    //  Created by Candy on 9/15/16.
    //  Copyright © 2016 Candy. All rights reserved.
    //
    
    #include <iostream>
    #include <cstdio>
    using namespace std;
    typedef long long ll;
    const ll N=1005,MOD=1e9+7;
    int n,p[N];
    ll d[N];
    void dp(){
        for(int i=1;i<=n;i++)
            d[i+1]=(d[i]+1+(d[i]-d[p[i]])+1+MOD)%MOD;
    }
    int main(int argc, const char * argv[]) {
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&p[i]);
        dp();
        cout<<d[n+1];
        return 0;
    }
  • 相关阅读:
    关于CSDN指针讨论的心得
    VC++ 6.0 与VS2008 C++ DEBUG工具(Windows)介绍
    VC++ 申明静态变量的注意事项
    大家好,我是新的blue1000~
    [讨论]当我采用动态sql绑定datagrid分页的时候,遇到的问题
    我与Google有个对话
    [BK专访]一切以客户为中心,其它一切纷至沓来
    [译]在.net中使用GDI+来提高gif图片的保存画质
    微软能不能别这样坑爹啊,即使不中毒,也伤不起啊
    长见识!1021字节javascript写成的3D圣诞树
  • 原文地址:https://www.cnblogs.com/candy99/p/5874793.html
Copyright © 2011-2022 走看看