結果

問題 No.3 ビットすごろく
ユーザー t8m8⛄️t8m8⛄️
提出日時 2015-04-03 07:07:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 175 ms / 5,000 ms
コード長 1,585 bytes
コンパイル時間 3,635 ms
コンパイル使用メモリ 77,068 KB
実行使用メモリ 56,952 KB
最終ジャッジ日時 2023-09-13 23:07:42
合計ジャッジ時間 10,085 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,736 KB
testcase_01 AC 125 ms
56,092 KB
testcase_02 AC 125 ms
56,132 KB
testcase_03 AC 141 ms
56,352 KB
testcase_04 AC 137 ms
56,540 KB
testcase_05 AC 143 ms
56,604 KB
testcase_06 AC 139 ms
56,476 KB
testcase_07 AC 138 ms
56,180 KB
testcase_08 AC 141 ms
55,672 KB
testcase_09 AC 145 ms
56,272 KB
testcase_10 AC 171 ms
56,680 KB
testcase_11 AC 143 ms
56,220 KB
testcase_12 AC 141 ms
55,964 KB
testcase_13 AC 139 ms
56,296 KB
testcase_14 AC 165 ms
56,696 KB
testcase_15 AC 167 ms
56,688 KB
testcase_16 AC 171 ms
56,676 KB
testcase_17 AC 175 ms
56,696 KB
testcase_18 AC 140 ms
56,396 KB
testcase_19 AC 172 ms
56,444 KB
testcase_20 AC 135 ms
56,088 KB
testcase_21 AC 123 ms
55,752 KB
testcase_22 AC 170 ms
56,868 KB
testcase_23 AC 174 ms
56,952 KB
testcase_24 AC 171 ms
56,616 KB
testcase_25 AC 174 ms
56,856 KB
testcase_26 AC 127 ms
55,492 KB
testcase_27 AC 140 ms
54,488 KB
testcase_28 AC 172 ms
56,728 KB
testcase_29 AC 141 ms
55,964 KB
testcase_30 AC 124 ms
55,432 KB
testcase_31 AC 126 ms
56,172 KB
testcase_32 AC 137 ms
56,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.lang.Math.*;

public class No3 {
    
    static final Scanner sc = new Scanner(System.in);
    static final PrintWriter out = new PrintWriter(System.out,false);

    static void solve() {
        int n = sc.nextInt();
        final int[] d = new int[n];
        fill(d,Integer.MAX_VALUE);
        d[0] = 1;
        TreeSet<Integer> que = new TreeSet<Integer>(new Comparator<Integer>(){
            public int compare(Integer a, Integer b) {
                if (d[a] != d[b]) return d[a] - d[b];
                return a - b;
            }
        });

        que.add(0);

        while (!que.isEmpty()) {
            int cur = que.pollFirst();
            int bit = Integer.bitCount(cur+1);
            if (cur - bit >= 0 && d[cur-bit] > d[cur] + 1) {
                d[cur-bit] = d[cur] + 1;
                que.remove(cur-bit);
                que.add(cur-bit);
            }
            if (cur + bit <n && d[cur + bit] > d[cur] + 1) {
                d[cur+bit] = d[cur] + 1;
                que.remove(cur+bit);
                que.add(cur+bit);
            }
        }

        out.println(d[n-1]==Integer.MAX_VALUE?"-1":d[n-1]);
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        solve();
        out.flush();

        long end = System.currentTimeMillis();
        //trace(end-start + "ms");
        sc.close();
    }

    static void trace(Object... o) { System.out.println(deepToString(o));}
}
0