当前位置: 首页 > news >正文

New Year Transportation(水)

New Year Transportation Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status

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

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.

题解:水题,就是i+a[i]能否到达。。。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <malloc.h>
using namespace std;
typedef struct type_data{
    int *a;
    int n, t;
    int ans;
    type_data(){
        ans = 2;
    }
}type_data;
void work(void *arg);
void *winit(){
    type_data* dt = (type_data *)malloc(sizeof(type_data));
    dt->a = (int *)malloc(sizeof(int) * 3e4 + 10);
    return (void *)dt;
}
void *wfinish(type_data* dt){
    delete(dt->a);
    dt->a = NULL;
    delete(dt);
    dt = NULL;
}
void io(type_data* dt){
    while(~scanf("%d%d", &dt->n, &dt->t)){
        for(int i = 1; i < dt->n; i++)
            scanf("%d", dt->a+i);
        work((void*)dt);
        if(dt->ans == 1)
            puts("YES");
        else if(!dt->ans)
            puts("NO");
        else
            puts("work error");
    }
}
void work(void* arg){
    type_data* dt = (type_data *)arg;
    dt->ans = 2;
    if(dt->t == 1)
        dt->ans = 1;
    for(int i = 1; i < dt->t && i < dt->n; ){
        if(i + dt->a[i] == dt->t)
            dt->ans = 1;
        i += dt->a[i];
    }
    if(dt->ans == 2)
        dt->ans = 0;
}    
int main(){
    type_data* dt = (type_data *)winit();
    io(dt);
    wfinish(dt);
    return 0;
}

 

转载于:https://www.cnblogs.com/handsomecui/p/5881307.html

相关文章:

  • 解决EditText和ScrollView滑动冲突问题
  • (转)fock函数详解
  • linux TLB表
  • 基于范围的for循环(STL)
  • bzoj1221: [HNOI2001] 软件开发
  • Python小杂点
  • nginx在 window下 自动退出 php-cgi
  • MongoDB 常用命令
  • 使用异或解题 —— 序列中仅出现一次的两个数
  • 为什么我从来不无偿加班?你也不应该! 【转载】
  • MySQL主从同步配置(Ubuntu)
  • C语言学习笔记--指针和数组的关系
  • css3样式二
  • 手机端轻应用模拟原生的下拉刷新效果(JavaScript)
  • 樱花漫地集于我心,蝶舞纷飞祈愿相随---总结 顕出:void-sampling 显示:void-sampling...
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • javascript数组去重/查找/插入/删除
  • java架构面试锦集:开源框架+并发+数据结构+大企必备面试题
  • React 快速上手 - 06 容器组件、展示组件、操作组件
  • Swift 中的尾递归和蹦床
  • Webpack 4 学习01(基础配置)
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 前端知识点整理(待续)
  • 实习面试笔记
  • 微信开源mars源码分析1—上层samples分析
  • 做一名精致的JavaScripter 01:JavaScript简介
  • 7行Python代码的人脸识别
  • 大数据全解:定义、价值及挑战
  • 格斗健身潮牌24KiCK获近千万Pre-A轮融资,用户留存高达9个月 ...
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • # 执行时间 统计mysql_一文说尽 MySQL 优化原理
  • (1)(1.11) SiK Radio v2(一)
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (4)logging(日志模块)
  • (4)STL算法之比较
  • (BFS)hdoj2377-Bus Pass
  • (TOJ2804)Even? Odd?
  • (含react-draggable库以及相关BUG如何解决)固定在左上方某盒子内(如按钮)添加可拖动功能,使用react hook语法实现
  • ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
  • .halo勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET 反射的使用
  • .net 怎么循环得到数组里的值_关于js数组
  • .NET设计模式(2):单件模式(Singleton Pattern)
  • .vollhavhelp-V-XXXXXXXX勒索病毒的最新威胁:如何恢复您的数据?
  • @Resource和@Autowired的区别
  • [Android 13]Input系列--获取触摸窗口
  • [Angular] 笔记 8:list/detail 页面以及@Input
  • [AUTOSAR][诊断管理][ECU][$37] 请求退出传输。终止数据传输的(上传/下载)
  • [C++核心编程](四):类和对象——封装
  • [CC2642R1][VSCODE+Embedded IDE+IAR Build+Cortex-Debug] TI CC2642R1基于VsCode的开发环境
  • [daily][archlinux][game] 几个linux下还不错的游戏
  • [Erlang 0129] Erlang 杂记 VI 2014年10月28日
  • [GN] 设计模式——面向对象设计原则概述