Good Bye 2014 -A. New Year Transportation

A. New Year Transportation

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 t by only using the construted transportation system.

Translation:
New Year is coming, queuing up the world! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in prison cells. However, because it is difficult to escape from the cell, it is difficult to move between different cells. People want to meet people living in other cells.

Therefore, user tncks0121 created a transportation system to move between these units to celebrate the New Year. First, he thought of n-1 positive integers a1, a2, …, n-1. For each integer i, the condition 1 ≤ i ≤ n-1 is 1 ≤ ai ≤ n-i. Next, he made n-1 portals, numbered as integers, from 1 to n-1. i (1 ≤ i ≤ n-1) The 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 that one cannot move from cell (i + ai) to cell i using the i-th portal. It is easy to see that because of the condition 1 ≤ ai ≤ n-i one cannot leave the online world to use the portal.

Currently, I am standing in Unit 1, and I want to go to the cell. However, I don't know if it is possible to go there. Please determine if I can only use the explained transportation system to get to the cell.

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.

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

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

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
inputCopy

8 5
1 2 1 2 1 1 1

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

Problem-solving ideas:

Keyword eyes: n-1 portals , using integer numbers from 1 to n-1, cell i and cell (i + ai), people using the i-th portal can start from cell i and travel Go to cell (i + ai) . But one cannot use the portal in reverse . Currently, you are standing at cell 1, and I want to go to cell t. Another thing is to know the transfer process between cells from the description. The loop body is mainly i+=a[i] . When i==t appears , it indicates that the cell t is found, exit the loop, and output "YES", and vice versa.
Insert picture description here

Reference Code

#include<iostream>
using namespace std;

int main(void)
{
    
    
    int n,t,i;
    cin>>n>>t;	//输入变量 n,t
    int a[n];

    for(int i=1;i<n;i++)
        cin>>a[i];	//输入1~(n-1)的数组变量值

   i=1;
   while(1){
    
    
        i+=a[i];	//主要计算操作
        if(i==t){
    
    		//当到达该单元格就输出”YES“并结束循环
            cout<<"YES"<<endl;
            break;
        }
        else if(i>t){
    
    	//当下标 i 已经超过 t 了,就表明无法到达该单元格,退出
            cout<<"NO"<<endl;
            break;
        }
   }
    return 0;
}

If you don’t learn it, welcome to interrupt;
if you learn, leave a thumbs up and encourage each other! ! !

Guess you like

Origin blog.csdn.net/weixin_45950429/article/details/113522412