結果

問題 No.3 ビットすごろく
ユーザー satori16satori16
提出日時 2016-08-14 10:55:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,822 bytes
コンパイル時間 671 ms
コンパイル使用メモリ 76,984 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 00:05:15
合計ジャッジ時間 1,990 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdint>
#include <limits>
#include <algorithm>
#include <tuple>

//解説読みました。Orz
//幅優先探索は難しいなぁ。

#define Test 0

typedef std::int16_t IntT;
typedef std::tuple<IntT, IntT> DType;
typedef std::vector<DType> Stack;
typedef std::vector<IntT> Stack2;
IntT BitCount(const IntT& v) {
	IntT C =0;

	for (IntT i = 0; i <= std::numeric_limits<IntT>::digits; i++) {
		C += (v&(1 << i)) > 0;
	}

	return C;
}

bool Rec(Stack2& S,const IntT& N,const IntT& V) {
	IntT B = BitCount(V);
	if (S.size() == 0) return false;
	if (V == N) return true;
	if (V > N)return false;
	if (V == 0) return false;
	
	//auto it = std::find(S.rbegin(), S.rend(), V+B);
	//if (it != S.rend()) return false;

	S.push_back(V + B);
	if (Rec(S, N, V + B) == true) return true;
	S.pop_back();
	auto it = std::find(S.rbegin(), S.rend(), V-B);
	if (it != S.rend()) return false;
	S.push_back(V - B);
	if (Rec(S, N,V - B) == true) return true;
	S.pop_back();
	return false;
}
/** /
IntT MakeHoge_2(const IntT& N) {
	Stack2 S;
	S.push_back(1);
	Rec(S, N,1);

	return S.back()==N? S.size():0;
}

/** /
std::size_t MakeHoge_1(const IntT& N) {
	Stack S;
	IntT C = 1;
	IntT B = 0;	
	S.push_back(std::make_tuple(C, 0));
	while (C != N && S.size() != 0) {
		if (std::get<1>(S.back()) == 2) {
			S.pop_back();
			continue;
		}
		C = std::get<0>(S.back());
		B = BitCount(C);
		if (std::get<1>(S.back()) == 0) {
			C += B;
		}
		else {
			C -= B;
		}
		std::get<1>(S.back())++;
		auto rit = std::find_if(S.rbegin(), S.rend(), [&](auto& o) {return std::get<0>(o) == C; });
		if (rit != S.rend()) {
			S.erase(rit.base(), S.rbegin().base());
		}
		else {
			if (C <= N) {
				S.push_back(std::make_tuple(C, 0));
			}
		}
	}
	return S.size();
}
/**/
IntT MakeHoge(const IntT& N) {
	IntT IV = N+2;
	Stack2 S(N+1, IV);
	IntT Now = 1;
	bool F = true;
	IntT B = 0;
	IntT C = 0;
	S[1] = 0;
	while (F) {
		F = false;
		for (IntT i = 1; i < S.size(); i++) {
			if (S[i] == IV) continue;
			B = BitCount(i);
			if (i - B > 0) {
				if (S[i - B] > S[i] + 1) {
					S[i - B] = S[i] + 1;
					F = true;
				}
			}
			if (i + B <= N) {
				if (S[i + B] > S[i] + 1) {
					S[i + B] = S[i] + 1;
					F = true;
				}
			}
		}
	}
	
	return S[N]==IV? -1:S[N]+1;

}

/**/
int main() {

	IntT R = 0;
	IntT N = 0;

#if Test
	N = 5;
	R = MakeHoge(N);
	std::cout << ((R == 0) ? -1 : R) << std::endl;
	N = 11;
	R = MakeHoge(N);
	std::cout << ((R == 0) ? -1 : R) << std::endl;
	N = 4;
	R = MakeHoge(N);
	std::cout << ((R == 0) ? -1 : R) << std::endl;
	N = 2343;
	R = MakeHoge(N);
	std::cout << ((R == 0) ? -1 : R) << std::endl;
	N = 4179;
	R = MakeHoge(N);
	std::cout << ((R == 0) ? -1 : R) << std::endl;
#else
	std::cin >> N;
	R = MakeHoge(N);
	std::cout << ((R == 0) ? -1 : R) << std::endl;
#endif

	return 0;
}
0