CF 500 A New Year Transportation

原创 2016年08月31日 15:36:04

题目连接:点击打开链接

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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

cf-500A New Year Transportation

A. New Year Transportation time limit per test 2 seconds memory limit per test 256 megab...
  • Floyld
  • Floyld
  • 2016年10月11日 21:52
  • 97

codeforces——500A—— New Year Transportation

A. New Year Transportation time limit per test 2 seconds memory limit per test 256 megabytes inpu...

Codeforces 500A:New Year Transportation(水题)

A. New Year Transportation time limit per test 2 seconds memory limit per test 256 megab...
  • zugofn
  • zugofn
  • 2016年09月28日 22:04
  • 241

【暴力】Codeforces-500A New Year Transportation

。。。暴力,看懂题目就行。 【题目】 New Year Transportation Time Limit:2000MS     Memory Limit:262144KB     ...

CF 500B New Year Permutation

B. New Year Permutation time limit per test2 seconds memory limit per test256 megabytes inputstan...

CF_500B New Year Permutation

B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes inpu...

线段树:CF500E E. New Year Domino

E. New Year Domino Celebrating the new year, many people post videos of falling dominoes; Here's...
  • NOI2015
  • NOI2015
  • 2015年01月29日 16:49
  • 555

【Good Bye 2014A】【水题 dfs】New Year Transportation 可否传送到t点

New Year Transportation time limit per test 2 seconds memory limit per test 256 megabytes inpu...

A. New Year Transportation

A. New Year Transportation time limit per test 2 seconds memory limit per test 256 megab...

【codeforces】New Year Transportation

New Year Transportation Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CF 500 A New Year Transportation
举报原因:
原因补充:

(最多只允许输入30个字)