zoukankan      html  css  js  c++  java
  • S

    Problem description

    New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.

    So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1 positive integers a1, a2, ..., an - 1. For every integer i where 1 ≤ i ≤ n - 1 the condition 1 ≤ ai ≤ n - i holds. Next, he made n - 1 portals, numbered by integers from 1 to n - 1. The i-th (1 ≤ i ≤ n - 1) portal connects cell i and cell (i + ai), and one can travel from cell i to cell (i + ai) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (i + ai) to cell i using the i-th portal. It is easy to see that because of condition 1 ≤ ai ≤ n - i one can't leave the Line World using portals.

    Currently, I am standing at cell 1, and I want to go to cell t. However, I don't know whether it is possible to go there. Please determine whether I can go to cell tby only using the construted transportation system.

    Input

    The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 104) and t (2 ≤ t ≤ n) — the number of cells, and the index of the cell which I want to go to.

    The second line contains n - 1 space-separated integers a1, a2, ..., an - 1 (1 ≤ ai ≤ n - i). It is guaranteed, that using the given transportation system, one cannot leave the Line World.

    Output

    If I can go to cell t using the transportation system, print "YES". Otherwise, print "NO".

    Examples

    Input

    8 4
    1 2 1 2 1 2 1

    Output

    YES

    Input

    8 5
    1 2 1 2 1 1 1

    Output

    NO

    Note

    In the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.

    In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to visit.

    解题思路:从cell1即f[1]出发,只要当前f[i]能到达,f[i+a[i]]就能到达,最后查看f[t]是否为true(表示已经访问过),是的话为"YES",否则为"NO"。

    AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int maxn=3e4+5;
     4 int n,t,a[maxn];bool f[maxn];
     5 int main(){
     6     cin>>n>>t;
     7     memset(f,false,sizeof(f));f[1]=true;
     8     for(int i=1;i<n;++i)cin>>a[i];
     9     for(int i=1;i<n;++i)
    10         if(f[i])f[i+a[i]]=true;
    11     if(f[t])cout<<"YES"<<endl;
    12     else cout<<"NO"<<endl;
    13     return 0;
    14 }
  • 相关阅读:
    python eval()函数
    Linux利用Sysctl命令调整内核参数
    Cookie测试的测试点
    python脚本对 MySQL 数据库进行增删改查
    页面加载经历的过程【转载】
    【转载】shell基础知识
    windows下查看端口占用情况
    Python的hashlib提供了常见的摘要算法
    Python统计文件夹下各个子目录中的文件个数
    Python读取文件下的图片格式
  • 原文地址:https://www.cnblogs.com/acgoto/p/9192713.html
Copyright © 2011-2022 走看看