結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
54,068 KB
testcase_01 AC 127 ms
54,008 KB
testcase_02 AC 129 ms
53,892 KB
testcase_03 AC 380 ms
63,776 KB
testcase_04 AC 207 ms
56,732 KB
testcase_05 AC 508 ms
63,668 KB
testcase_06 AC 375 ms
63,648 KB
testcase_07 AC 294 ms
61,020 KB
testcase_08 AC 442 ms
63,776 KB
testcase_09 AC 630 ms
52,272 KB
testcase_10 AC 778 ms
53,060 KB
testcase_11 AC 584 ms
52,612 KB
testcase_12 AC 491 ms
52,296 KB
testcase_13 AC 366 ms
51,480 KB
testcase_14 AC 746 ms
52,680 KB
testcase_15 AC 963 ms
53,056 KB
testcase_16 WA -
testcase_17 AC 965 ms
52,912 KB
testcase_18 AC 333 ms
51,532 KB
testcase_19 AC 995 ms
52,736 KB
testcase_20 AC 182 ms
43,672 KB
testcase_21 AC 134 ms
41,448 KB
testcase_22 AC 752 ms
52,744 KB
testcase_23 AC 1,002 ms
52,904 KB
testcase_24 AC 995 ms
52,816 KB
testcase_25 AC 989 ms
52,828 KB
testcase_26 AC 131 ms
41,440 KB
testcase_27 AC 361 ms
52,380 KB
testcase_28 AC 873 ms
52,556 KB
testcase_29 AC 587 ms
52,580 KB
testcase_30 AC 136 ms
41,468 KB
testcase_31 AC 152 ms
42,284 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 = 0;
					continue;
				}
				av.put(i - bit, Math.min(minD + 1, av.get(i - bit)));
			}
		}

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

}
0