您的位置:首页 > 其它

CodeForces 500A New Year Transportation

2016-07-20 17:07 429 查看
E - New Year Transportation
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
500A

Appoint description: 
System Crawler  (Jul 14, 2016 8:28:17 AM)

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

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

Sample Input

Input
8 4
1 2 1 2 1 2 1


Output
YES


Input
8 5
1 2 1 2 1 1 1


Output
NO


Hint

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.

读懂题后首先想到了并查集,不过这道题不用这么麻烦,模拟一下即可

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX 0xfffffff
using namespace std;
int main()
{
int a[31000];
int n,t,i,j,m;
while(scanf("%d%d",&n,&t)!=EOF)
{
memset(a,0,sizeof(a));
for(i=1;i<=n-1;i++)
scanf("%d",&a[i]);
a
=MAX;//注意
int i=1;
int step=1;
bool flag=false;
while(step<=n)
{
if(step==t)
{
flag=true;
break;
}
step+=a[i];
i=step;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: