0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Atcoder解いてみた AtCoder Beginner Contest D - Step Up Robot

0
Posted at

AtCoder Beginner Contest D - Step Up Robot

問題はこちら

回答

O(NM)までは許される
状態遷移は以下

  • i段目からN種類の段数に飛べる

最終的に、X段目に到達できるか否かを求めたいため、以下のdp配列を使用する
dp[i] : i段目に到達できるか否か
i段目からN種類の段数に飛ぶため、i→i+A[i]のように配るDPを行う

※もちがおかれている段にも到達できる可能性はあることに注意(到達したらそこから動けなくなるだけ)

#include <iostream>
#include <string>
#include <map>
#include <unordered_map>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <limits.h>
#include <bitset>
#include <list>
#include <set>
#include <numeric>
#include <tuple>
#include <iomanip>
#include <random>


int N, M, X;
std::vector<int> A;
std::vector<int> B;

int main()
{
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);

	std::cin >> N;
	A.resize(N);
	for (int i = 0; i < N; i++) {
		std::cin >> A[i];
	}

	std::cin >> M;
	B.resize(M);
	for (int i = 0; i < M; i++) {
		std::cin >> B[i];
	}

	std::cin >> X;

	// x段目に到達できるか否か
	std::vector<int> dp;
	std::vector<int> pre;

	// -1 : 初期値, 0 : もちの段, 1 : 到達可能(もちじゃない), 2 : 到達可能(もちの段)
	dp.resize(1000000, -1);

	// 0段目には到達できる
	dp[0] = 1;

	// もちの段を設定
	for (int i = 0; i < M; i++) {
		dp[B[i]] = 0;
	}

	// もちの段に行くと、動けなくなるだけで、到達はできる可能性あり
	// x段目までみた
	for (int x = 0; x <= 100000; x++) {

		// N通りの動作パターンを試す
		for (int n = 0; n < N; n++) {

			// 初期値 or もちの段 or 到達可能(もちの段)
			if (dp[x] == -1 || dp[x] == 0 || dp[x] == 2) {
				continue;
			}

			int dst = x + A[n];

			//	到達先がもちの段
			if (dp[dst] == 0 || dp[dst] == 2) {
				dp[dst] = 2;
			}
			else {
				dp[dst] = 1;
			}
		}
	}

	if (dp[X] == 1 || dp[X] == 2) {
		std::cout << "Yes" << std::endl;
	}
	else {
		std::cout << "No" << std::endl;
	}

	return 0;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?