結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2019-12-18 11:20:30 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 76 ms / 5,000 ms |
コード長 | 1,258 bytes |
コンパイル時間 | 2,354 ms |
コンパイル使用メモリ | 78,024 KB |
実行使用メモリ | 37,716 KB |
最終ジャッジ日時 | 2024-07-01 09:35:33 |
合計ジャッジ時間 | 5,243 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] arr = new int[n + 1]; Arrays.fill(arr, -1); ArrayDeque<Integer> deq = new ArrayDeque<>(); ArrayDeque<Integer> next = new ArrayDeque<>(); deq.add(1); int count = 0; while (deq.size() > 0) { count++; while (deq.size() > 0) { int x = deq.poll(); if (arr[x] == -1) { arr[x] = count; int y = getCount(x); if (x + y <= n) { next.add(x + y); } if (x - y > 0) { next.add(x - y); } } } ArrayDeque<Integer> tmp = deq; deq = next; next = tmp; } System.out.println(arr[n]); } static int getCount(int x) { int count = 0; while (x > 0) { count += x % 2; x /= 2; } return count; } }