zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    转 自学编程这几点非常重要
    转 错误:ORA-28002/ORA-65162 : the password will expire within 7 days 解决方法
    Python基础3
    Python基础2
    Python基础1
    爬虫实战项目集合
    Python3中PyMongo的用法
    难点--均方误差(MSE)和均方根误差(RMSE)和平均绝对误差(MAE)
    Excel合并
    mongoDB常用操作命令
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5881307.html
Copyright © 2011-2022 走看看