zoukankan      html  css  js  c++  java
  • cf div2 239 D

    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.

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 typedef long long ll;
     9 
    10 const int MAX_N = 1e3 + 5;
    11 const int MOD = 1e9 + 7;
    12 int N;
    13 int p[MAX_N],sta[MAX_N],dp[2 * MAX_N];
    14 
    15 int cal(int x) {
    16         return (!sta[x]) * (N + 1) + x;
    17 }
    18 
    19 int main()
    20 {
    21     scanf("%d",&N);
    22     fill(dp + 1,dp + 2 * (N + 1) + 1,-1);
    23     for(int i = 1; i <= N; ++i) {
    24             scanf("%d",&p[i]);
    25     }
    26 
    27     sta[1] = 1;
    28     int now = 1,ans = 0,next;
    29     while(now != N + 1) {
    30             if(dp[cal(now)] == -1)
    31             dp[ cal(now)] = ans;
    32             if(sta[now] % 2) {
    33                     next = p[now];
    34             } else {
    35                     next = now + 1;
    36             }
    37          
    38             int nowid = cal(now),nextid;
    39             sta[next] = !sta[next];
    40             nextid = cal(next);
    41             if( dp[ cal(next) ] != -1) {
    42                     ans = ( (ll)ans + (dp[nowid] - dp[nextid]) + MOD + 1 ) % MOD;
    43                     sta[now] = (sta[now] + 1) % 2;
    44                     sta[next] = !sta[next];
    45             } else {
    46                     now = next;
    47                     ans = (ans + 1) % MOD;
    48             }
    49     }
    50 
    51  
    52 
    53     printf("%d
    ",ans);
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    var type = $('#<%=DropDownListRateType.ClientID %>').val();DropDownListRateType.ClientID是什么意思
    通过代码理解Asp.net4中的几种ClientIDMode设置.
    left join right join inner join 详解
    T-SQL查询处理执行顺序(一)
    Tsql查询执行顺序(二)
    【转】"超时时间已到。在操作完成之前超时时间已过或服务器未响应"的解决方法
    【转】ibatis 中使用select top #pagesize# * from tablename
    最近找到的一些分页优化的方法
    看懂SqlServer查询计划
    java中的数据结构
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3658704.html
Copyright © 2011-2022 走看看