• codeforces 500A. New Year Transportation


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

    题目意思:给出 n-1 个 cell,每个 cell 有一个值 ai,表示在这个编号为 i 的 cell,能到达i + ai 的cell,但不能反过来,即从 i+ai 到达 i 这个 cell。问从第一个cell 开始,是否可以到达 t 这个cell。

      第一次过不了pretest 是因为没有考虑到,如果 t = 1的情况,后来被人 hack 之后就不知道原因了。。。原来是因为第 n 个 cell,默认是 0,应该赋予一个很大的数值!!注意题目只给出 1 ~ n-1 cell 的 ai , 是没有给出 第 n 个的!!!所以要设值,否则代码中的while 会变成死循环的。

      

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 const int maxn = 3e4 + 10;
     9 
    10 int p[maxn];
    11 
    12 int main()
    13 {
    14     #ifndef ONLINE_JUDGE
    15         freopen("in.txt", "r", stdin);
    16     #endif // ONLINE_JUDGE
    17     int n, t;
    18     while (scanf("%d%d", &n, &t) != EOF)
    19     {
    20         for (int i = 1; i <= n-1; i++)
    21             scanf("%d", &p[i]);
    22         p[n] = maxn;         // 这个很关键!
    23         bool flag = false;
    24         int i = 1;
    25         int step = 1;        // 初始化为1方便进入while循环的if判断(有可能t==1)
    26         while (step <= n)
    27         {
    28             if (step == t)
    29             {
    30                 flag = true;
    31                 break;
    32             }
    33             step += p[i];
    34             i = step;
    35         }
    36         printf("%s
    ", flag ? "YES" : "NO");
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    Java基础知识【上】(转载)
    Windows下Python中pip安装Pillow报错总结(转载)
    CentOS7下安装Python的pip
    CentOS7安装NodeJS6.9
    PostGIS(解压版)安装
    CentOS7中安装Python3.5
    CentOS7安装docker
    Centos7更改默认启动模式(转载)
    CentOS7 查看IP、Gateway、DNS、Hostname
    CentOS7系统安装及初始化
  • 原文地址:https://www.cnblogs.com/windysai/p/4196857.html
Copyright © 2020-2023  润新知