結果

問題 No.3 ビットすごろく
ユーザー OnjuOnju
提出日時 2015-01-31 05:42:42
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 714 bytes
コンパイル時間 589 ms
コンパイル使用メモリ 63,940 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 08:30:58
合計ジャッジ時間 3,608 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 9 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 39 ms
4,380 KB
testcase_06 AC 11 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 24 ms
4,376 KB
testcase_09 AC 63 ms
4,376 KB
testcase_10 AC 90 ms
4,376 KB
testcase_11 AC 52 ms
4,380 KB
testcase_12 AC 35 ms
4,380 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 85 ms
4,380 KB
testcase_15 AC 130 ms
4,380 KB
testcase_16 AC 114 ms
4,380 KB
testcase_17 AC 128 ms
4,376 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 135 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 87 ms
4,376 KB
testcase_23 AC 134 ms
4,376 KB
testcase_24 AC 134 ms
4,380 KB
testcase_25 AC 131 ms
4,376 KB
testcase_26 WA -
testcase_27 AC 8 ms
4,376 KB
testcase_28 AC 109 ms
4,380 KB
testcase_29 AC 52 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 46 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//No.3 ビットすごろく

#include <iostream>
#include <unordered_set>
#include <limits.h>
#include <bitset>

#define NMAX 10000

using namespace std;

int C[NMAX];
int N;

int cal(int val, int cost)
{
	++cost;

	bitset<32> bs(val);
	int count = bs.count();

	int next_l = val - count;
	int next_r = val + count;

	if (next_l > 0)
		if (cost < C[next_l - 1])
		{
			C[next_l - 1] = cost;
			cal(next_l, cost);
		}
	if (next_r <= N)
		if (cost < C[next_r - 1])
		{
			C[next_r - 1] = cost;
			cal(next_r, cost);
		}

	return val;
}

int solve()
{
	cal(1, 1);

	return C[N - 1] < INT_MAX ? C[N - 1] : -1;
}

int main()
{
	cin >> N;

	for (int i = 0; i < N; ++i)
		C[i] = INT_MAX;

	cout << solve();

	return 0;
}
0