关闭

CF 500 A New Year Transportation

标签: acmcfbfs
149人阅读 评论(0) 收藏 举报
分类:

题目连接:点击打开链接

A. New Year Transportation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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".

Examples
Input
8 4
1 2 1 2 1 2 1
Output
YES
Input
8 5
1 2 1 2 1 1 1
Output
NO
Note

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.

题意:看不太懂,大体就是n个城市编号1~n,前n-1个城市分别有一个通道,每个通道长ai,即从第i个城市可以到达第i+ai个城市,现给出这n-1个通道的长度,并给出一个城市的编号t,问是否能从1城市到t城市

思路:比较水,时间允许,挨个试就行。这也是bfs的基本思路吧


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<functional> 
#include<map>
using namespace std;
const int maxn = 30000 + 10;
const int INF = (int)1e9;

int n, t;
int a[maxn];

int main() {
	scanf("%d %d", &n, &t);
	for (int i = 0; i < n - 1; i++)
		scanf("%d", &a[i]);
	int sum = 0;
	t--;
	while (sum < t) {
		sum += a[sum];
	}
	if (sum == t)printf("YES\n");
	else printf("NO\n");

	//system("pause");
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:29999次
    • 积分:1812
    • 等级:
    • 排名:千里之外
    • 原创:159篇
    • 转载:9篇
    • 译文:0篇
    • 评论:4条
    最新评论