zoukankan      html  css  js  c++  java
  • CodeForces 500 A. New Year Transportation

    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.

    题目链接:http://codeforces.com/problemset/problem/500/A

    ************************************************

    题意:给你n个数下标为1~n,每个位置给的a[n]就是站在这个位置接着可以走的步数,要求你判断是否可以访问到下标为t的位置。

    分析:直接模拟,然后进行判断就好。

    AC代码:

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 #include<queue>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<iostream>
     8 
     9 using namespace std;
    10 typedef long long LL;
    11 
    12 #define INF 0x3f3f3f3f
    13 #define N 41000
    14 #define MAXN 100000000
    15 #define mod 1000000007
    16 
    17 int a[N];
    18 
    19 int main()
    20 {
    21     int n,t,i;
    22 
    23     while(scanf("%d%d", &n,&t) != EOF)
    24     {
    25         for(i=1; i<n; i++)
    26             scanf("%d", &a[i]);
    27 
    28         i=1;
    29         while(1)
    30         {
    31             i=i+a[i];
    32             if(i==t)
    33             {
    34                 printf("YES
    ");
    35                 break;
    36             }
    37             if(i>t)
    38             {
    39                 printf("NO
    ");
    40                 break;
    41             }
    42         }
    43 
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    素数
    超级素数
    SUMMARIZE 6.1
    广度优先搜索与八字码问题
    poj2352
    poj1198
    康托展开
    STL里的内存池实现
    构造函数,C++内存管理,内存泄漏定位
    内联函数,宏定义,内存对齐,类型转换
  • 原文地址:https://www.cnblogs.com/weiyuan/p/5794882.html
Copyright © 2011-2022 走看看