結果

問題 No.3 ビットすごろく
ユーザー tentententen
提出日時 2020-11-16 18:15:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 156 ms / 5,000 ms
コード長 1,382 bytes
コンパイル時間 2,100 ms
コンパイル使用メモリ 75,168 KB
実行使用メモリ 56,428 KB
最終ジャッジ日時 2023-09-14 01:54:25
合計ジャッジ時間 7,221 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
56,256 KB
testcase_01 AC 106 ms
56,272 KB
testcase_02 AC 108 ms
55,928 KB
testcase_03 AC 113 ms
56,428 KB
testcase_04 AC 113 ms
56,016 KB
testcase_05 AC 135 ms
56,020 KB
testcase_06 AC 114 ms
55,700 KB
testcase_07 AC 111 ms
56,280 KB
testcase_08 AC 133 ms
55,716 KB
testcase_09 AC 132 ms
56,068 KB
testcase_10 AC 134 ms
56,044 KB
testcase_11 AC 136 ms
55,820 KB
testcase_12 AC 141 ms
55,884 KB
testcase_13 AC 112 ms
56,132 KB
testcase_14 AC 125 ms
56,312 KB
testcase_15 AC 126 ms
56,088 KB
testcase_16 AC 137 ms
55,808 KB
testcase_17 AC 125 ms
56,028 KB
testcase_18 AC 110 ms
56,164 KB
testcase_19 AC 139 ms
55,748 KB
testcase_20 AC 109 ms
56,252 KB
testcase_21 AC 106 ms
56,104 KB
testcase_22 AC 127 ms
56,088 KB
testcase_23 AC 126 ms
56,392 KB
testcase_24 AC 139 ms
56,152 KB
testcase_25 AC 130 ms
56,128 KB
testcase_26 AC 107 ms
55,492 KB
testcase_27 AC 113 ms
56,032 KB
testcase_28 AC 138 ms
55,748 KB
testcase_29 AC 153 ms
55,840 KB
testcase_30 AC 112 ms
55,700 KB
testcase_31 AC 125 ms
55,784 KB
testcase_32 AC 156 ms
55,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] costs = new int[n + 1];
        Arrays.fill(costs, Integer.MAX_VALUE);
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(1, 1));
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (p.idx < 0 || p.idx > n || costs[p.idx] <= p.value) {
                continue;
            }
            costs[p.idx] = p.value;
            int count = getCount(p.idx);
            queue.add(new Path(p.idx + count, p.value + 1));
            queue.add(new Path(p.idx - count, p.value + 1));
        }
        if (costs[n] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(costs[n]);
        }
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
    
    static int getCount(int x) {
        int count = 0;
        while (x > 0) {
            count += x % 2;
            x /= 2;
        }
        return count;
    }
}
0