結果

問題 No.3 ビットすごろく
ユーザー t8m8⛄️t8m8⛄️
提出日時 2015-04-03 07:07:04
言語 Java
(openjdk 23)
結果
AC  
実行時間 178 ms / 5,000 ms
コード長 1,585 bytes
コンパイル時間 3,997 ms
コンパイル使用メモリ 79,792 KB
実行使用メモリ 54,840 KB
最終ジャッジ日時 2024-07-01 07:19:14
合計ジャッジ時間 9,940 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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