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 }
  • 相关阅读:
    修改Tomcat可支持get形式url长度
    UTF-8 带签名和不带签名的区别
    注册asp.net 4.0版本到IIS服务器中
    C#计算字符串长度,汉字算两个字符
    高德地图Javascript API设置域名白名单
    金三银四招聘季,这些BAT以及独角兽互联网公司官方招聘网站值得关注。(个人梳理备用:附BAT以及独角兽公司官方招聘网址)
    【转载】 C#工具类:Csv文件转换类
    【转载】ASP.NET生成图片的缩略图
    【转载】IIS报错不是有效的Win32应用程序
    【转载】C#工具类:FTP操作辅助类FTPHelper
  • 原文地址:https://www.cnblogs.com/weiyuan/p/5794882.html
Copyright © 2011-2022 走看看