A. New Year Transportation
time limit per test
memory limit per test
input
output
n cells numbered by integers from 1 to n, as a 1 × n
n - 1positive 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 - 1portals, 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
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
Input
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.
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
t using the transportation system, print "YES". Otherwise, print "NO".
Sample test(s)
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
1, 2, 4; so we can successfully visit the cell 4.
1, 2, 4, 6, 7, 8; so we can't visit the cell 5, which we want to visit.
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
int a[30010];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;)
{
i = i + a[i];
if(i == m)
{
printf("YES\n");
break;
}
if(i>m)
{
printf("NO\n");
break;
}
}
}
return 0;
}