1.24 A

A - 1

Time limit 2000 ms
Memory limit 262144 kB

Problem 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

8 4
1 2 1 2 1 2 1

8 5
1 2 1 2 1 1 1

Sample Output

YES

NO

问题链接:A - 1

问题简述:

输入n,t分别代表牢房的数目和要转到的那个牢房,再输入n-1个数代表在这个牢房只能继续走多少步,问能不能走到第t个牢房

问题分析:

我们来看sample input
8 5
1 2 1 2 1 1 1
8个牢房,要去第5个牢房,一开始在第一个牢房、
首先1+1=2,此时你在第二个牢房(1 2 1 2 1 1 1)
然后2+2=4,此时你在第四个牢房(1 2 1 2 1 1 1)
然后4+2=6,此时你在第六个牢房(1 2 1 2 1 1 1)
然后6+1=7,此时你在第七个牢房(1 2 1 2 1 1 1
然后7+1=8,此时你在第八个牢房

程序说明:

没啥好说的,读懂题目是关键

AC通过的C语言程序如下:

#include <iostream>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int aim;
	cin >> aim;
	int a[100000];
	int sum;
	for (int i = 1;i < n;i++)
	{
		cin >> a[i];
	}
	for (int i = 1;i < n;)
	{
		sum = i + a[i];
		i = sum;
		if (i == aim)
		{
			cout << "YES";goto L;
		}
	}
	cout << "NO";
L:;
}

猜你喜欢

转载自blog.csdn.net/weixin_44003969/article/details/86628697