CUC-SUMMER-8-A

A - New Year Transportation
CodeForces - 500A

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.

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

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


题意:一个一维的轴上,你在1位置处,每一个坐标有一个权值代表向前移动的距离,你有一个目的地,问能不能到达

解法:水题,从1开始模拟即可,如果到达目的坐标或者超过目的坐标则循环终止。

代码:

#include<iostream>
using namespace std;
int a[30005];
int main()
{
    int n,t,s;
    cin>>n>>t;
    for(int i=1;i<n;i++)
        cin>>a[i];
    s=1;
    while(1){
        if(s==t){
            cout<<"YES"<<endl;
            break;
        }
        if(s>t){
            cout<<"NO"<<endl;
            break;
        }
        s+=a[s];
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 无论你是吉他初学者还是已经学了一点弹唱吉他,无论你是金属吉他手还是乡村吉他手,总有一天都会有人跟你说:“来一起Ja...
    吉他范儿阅读 5,166评论 0 0
  • 心情不好,有点难受,不想睡觉,烦躁,做不下决定
    胖成一个墩儿阅读 2,472评论 0 0
  • 一次偶然的相遇 你来到我的生命 许下相伴一生的承诺 一颗孤独的心从此有了依靠 温柔善良的你犹如一缕阳光 照进了 ...
    思y阅读 2,593评论 0 0
  • 自己好像经常这样,每隔一段时间就会失眠一次。平时忙的时候,因为没空去思考,所以总觉得那时的自己是强大的,心中的问题...
    摆摆O_o阅读 1,459评论 0 0

友情链接更多精彩内容