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


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

UVA 301 Transportation 。。有点需要思考的回溯、、

UVA 301 Transportation  Transportation  Ruratania is just entering capitalism and is es...
  • u011217342
  • u011217342
  • 2013年07月20日 23:56
  • 1246

【CF 708C】Centroids(树型DP)

C. Centroidstime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputs...
  • ChallengerRumble
  • ChallengerRumble
  • 2016年08月25日 14:23
  • 1168

CF 149D Coloring Brackets(区间DP,好题,给配对的括号上色,求上色方案数,限制条件多,dp四维)

1、http://codeforces.com/problemset/problem/149/D 2、题目大意 给一个给定括号序列,给该括号上色,上色有三个要求 1、只有三种上色方案,不上色,上...
  • sdjzping
  • sdjzping
  • 2014年02月13日 14:15
  • 3119

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
  • 145

codeforces——500A—— New Year Transportation

A. New Year Transportation time limit per test 2 seconds memory limit per test 256 megabytes inpu...
  • lose__way
  • lose__way
  • 2017年05月15日 19:37
  • 90

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
  • 281

【暴力】Codeforces-500A New Year Transportation

。。。暴力,看懂题目就行。 【题目】 New Year Transportation Time Limit:2000MS     Memory Limit:262144KB     ...
  • silent0001
  • silent0001
  • 2016年09月18日 18:22
  • 179

CF 500B New Year Permutation

B. New Year Permutation time limit per test2 seconds memory limit per test256 megabytes inputstan...
  • q779160073
  • q779160073
  • 2015年08月04日 13:42
  • 399

CF_500B New Year Permutation

B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes inpu...
  • EaShion1994
  • EaShion1994
  • 2015年01月11日 21:08
  • 575

线段树: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
  • 573
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CF 500 A New Year Transportation
举报原因:
原因补充:

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