Good Bye 2014--A. New Year Transportation

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 - 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 - 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 celli 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 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

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.




题意:给出n和m,给出1~n-1位置的跳跃值a[i],位置t的初始值为1,每到达一个位置i,就可以往后跳跃到t + a[i]的位置上,依次循环,直到把所有的位置都跳过,注意,只能向后跳。问从开始能否恰好跳到位置m。


解析:直接暴力处理,看是否恰好走到即可。




AC代码:

非数组版的:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define INF 0x7fffffff
#define LL long long
#define LLL(a, b)  a<<b
#define RRR(a, b)  a>>b
#define MID(a, b)  a+(b-a)/2
const int maxn = 30000 + 10;

int a[maxn];

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif // sxk

    int n, m, x;
    while(scanf("%d%d", &n, &m)!=EOF){
        int t = 1, flag = 0;
        for(int i=1; i<n; ){
            if(t == m)  flag = 1;
            scanf("%d", &x);
            t += x;
            int y = x - 1;
            while(y--) scanf("%d", &x);
            i = t;
        }
        if(n == m) flag = 1;
        printf("%s\n", flag ? "YES" : "NO");
    }
    return 0;
}



数组版的:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define INF 0x7fffffff
#define LL long long
#define LLL(a, b)  a<<b
#define RRR(a, b)  a>>b
#define MID(a, b)  a+(b-a)/2
const int maxn = 30000 + 10;

int a[maxn];

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif // sxk

    int n, m, x;
    while(scanf("%d%d", &n, &m)!=EOF){
        int t = 1, flag = 0;
        for(int i=1; i<n; i++) scanf("%d", &a[i]);
        for(int i=1; i<n; ){
            t += a[i];
            i = t;
            if(t == m){
                flag = 1;
                break;
            } 
        }
        printf("%s\n", flag ? "YES" : "NO");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值