結果

問題 No.3 ビットすごろく
ユーザー mitsuomitsuo
提出日時 2016-05-05 17:28:44
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,241 bytes
コンパイル時間 2,337 ms
コンパイル使用メモリ 80,512 KB
実行使用メモリ 58,428 KB
最終ジャッジ日時 2024-04-15 13:18:13
合計ジャッジ時間 8,776 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
54,296 KB
testcase_01 AC 133 ms
54,176 KB
testcase_02 AC 134 ms
54,260 KB
testcase_03 AC 166 ms
54,464 KB
testcase_04 WA -
testcase_05 AC 159 ms
56,216 KB
testcase_06 AC 160 ms
54,320 KB
testcase_07 AC 160 ms
54,408 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 175 ms
56,516 KB
testcase_13 AC 147 ms
54,344 KB
testcase_14 AC 183 ms
56,688 KB
testcase_15 AC 179 ms
56,992 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 146 ms
54,336 KB
testcase_19 WA -
testcase_20 AC 137 ms
54,048 KB
testcase_21 AC 133 ms
54,260 KB
testcase_22 AC 180 ms
56,796 KB
testcase_23 AC 184 ms
57,152 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 134 ms
53,836 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 135 ms
54,060 KB
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package jp.fedom.challange.yuki.l2.q3;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		List<String> input = new ArrayList<>();
		while (sc.hasNext()) {
			input.add(sc.nextLine());
		}

		System.out.println(solve(input));

		sc.close();
	}

	public static int solve(List<String> in) {
		int N = Integer.valueOf(in.get(0));
		Map<Integer, Integer> av = new HashMap<Integer, Integer>();

		av.put(0, 0);
		av.put(1, 1);
		for (int i = 0; i <= N; i++) {
			// System.out.println(av + " / " + i);
			int bit = BigInteger.valueOf(i).bitCount();
			if (!av.containsKey(i)) {
				continue;
			}

			int minD = av.get(i);

			if (i + bit <= N) {
				if (!av.containsKey(i + bit)) {
					av.put(i + bit, minD + 1);
				}
				av.put(i + bit, Math.min(minD + 1, av.get(i + bit)));
			}

			if (i - bit >= 0) {
				if (!av.containsKey(i - bit)) {
					av.put(i - bit, minD + 1);
					i = i - bit - 1;
					continue;
				}
				av.put(i - bit, Math.min(minD + 1, av.get(i - bit)));
			}
		}

		return av.containsKey(N) ? av.get(N) : -1;
	}

}
0