結果

問題 No.3 ビットすごろく
ユーザー tentententen
提出日時 2022-07-28 16:06:46
言語 Java19
(openjdk 21)
結果
AC  
実行時間 73 ms / 5,000 ms
コード長 1,956 bytes
コンパイル時間 3,262 ms
コンパイル使用メモリ 74,996 KB
実行使用メモリ 51,648 KB
最終ジャッジ日時 2023-09-25 08:25:06
合計ジャッジ時間 6,067 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,668 KB
testcase_01 AC 44 ms
49,436 KB
testcase_02 AC 44 ms
49,548 KB
testcase_03 AC 51 ms
49,816 KB
testcase_04 AC 48 ms
49,640 KB
testcase_05 AC 70 ms
51,368 KB
testcase_06 AC 53 ms
49,716 KB
testcase_07 AC 50 ms
49,820 KB
testcase_08 AC 54 ms
49,724 KB
testcase_09 AC 63 ms
51,588 KB
testcase_10 AC 71 ms
51,204 KB
testcase_11 AC 61 ms
51,188 KB
testcase_12 AC 61 ms
51,396 KB
testcase_13 AC 50 ms
49,564 KB
testcase_14 AC 71 ms
51,620 KB
testcase_15 AC 73 ms
51,648 KB
testcase_16 AC 62 ms
51,456 KB
testcase_17 AC 71 ms
51,216 KB
testcase_18 AC 49 ms
49,684 KB
testcase_19 AC 71 ms
51,300 KB
testcase_20 AC 46 ms
49,612 KB
testcase_21 AC 43 ms
49,548 KB
testcase_22 AC 61 ms
51,212 KB
testcase_23 AC 72 ms
51,304 KB
testcase_24 AC 71 ms
51,064 KB
testcase_25 AC 62 ms
51,324 KB
testcase_26 AC 43 ms
49,520 KB
testcase_27 AC 50 ms
50,256 KB
testcase_28 AC 59 ms
51,240 KB
testcase_29 AC 60 ms
51,464 KB
testcase_30 AC 43 ms
49,444 KB
testcase_31 AC 44 ms
47,528 KB
testcase_32 AC 69 ms
51,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] counts = new int[n + 1];
        Arrays.fill(counts, -1);
        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 || counts[p.idx] != -1) {
                continue;
            }
            counts[p.idx] = p.value;
            int pop = getPop(p.idx);
            queue.add(new Path(p.idx + pop, p.value + 1));
            queue.add(new Path(p.idx - pop, p.value + 1));
        }
        System.out.println(counts[n]);
    }
    
    static int getPop(int x) {
        int pop = 0;
        while (x > 0) {
            pop += x % 2;
            x >>= 1;
        }
        return pop;
    }
    
    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;
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0