結果

問題 No.3 ビットすごろく
ユーザー shiratamashiratama
提出日時 2017-05-06 16:23:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 420 ms / 5,000 ms
コード長 979 bytes
コンパイル時間 522 ms
コンパイル使用メモリ 66,660 KB
実行使用メモリ 8,468 KB
最終ジャッジ日時 2023-09-14 00:42:56
合計ジャッジ時間 5,381 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 11 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 67 ms
4,376 KB
testcase_10 AC 164 ms
4,508 KB
testcase_11 AC 40 ms
4,380 KB
testcase_12 AC 9 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 136 ms
4,656 KB
testcase_15 AC 358 ms
6,936 KB
testcase_16 AC 214 ms
4,664 KB
testcase_17 AC 333 ms
6,936 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 420 ms
7,852 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 146 ms
4,636 KB
testcase_23 AC 412 ms
8,468 KB
testcase_24 AC 409 ms
8,072 KB
testcase_25 AC 368 ms
7,340 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 200 ms
4,796 KB
testcase_29 AC 41 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 26 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <limits>
#include <queue>
#include <vector>

struct Step {
	int curr_n;
	int cost;
};

auto count_one(int n) -> int {
	int count = 0;
	for (int b = 1; b <= 10000; b <<= 1) {
		if ((n & b) != 0) {
			count++;
		}
	}
	return count;
}

auto main() -> int {
	int last_n;
	std::cin >> last_n;

	std::queue<Step> steps;
	std::vector<int> costs(last_n, std::numeric_limits<int>::max());

	steps.push({1, 1});
	while (!steps.empty()) {
		Step s = steps.front();
		steps.pop();

		if (s.cost <= costs[s.curr_n - 1]) {
			costs[s.curr_n - 1] = s.cost;

			int n_one = count_one(s.curr_n);
			if ((s.curr_n + n_one) <= last_n) {
				steps.push({s.curr_n + n_one, s.cost + 1});
			}
			if ((s.curr_n - n_one) >= 1) {
				steps.push({s.curr_n - n_one, s.cost + 1});
			}
		}
	}

	int smallestCost = costs[last_n - 1];
	if (smallestCost == std::numeric_limits<int>::max()) {
		std::cout << -1 << std::endl;
	} else {
		std::cout << smallestCost << std::endl;
	}
}
0