結果
問題 | No.3 ビットすごろく |
ユーザー | holeguma |
提出日時 | 2015-07-26 02:17:03 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 59 ms / 5,000 ms |
コード長 | 775 bytes |
コンパイル時間 | 2,763 ms |
コンパイル使用メモリ | 78,064 KB |
実行使用メモリ | 37,224 KB |
最終ジャッジ日時 | 2024-07-01 07:27:57 |
合計ジャッジ時間 | 5,119 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
import java.io.*; import java.util.Arrays; import java.util.Deque; import java.util.ArrayDeque; class Main{ final static int INF=Integer.MAX_VALUE/2; public static void main(String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String line=""; while((line=br.readLine())!=null){ int t=Integer.parseInt(line); int[] map=new int[t+1]; Arrays.fill(map,INF); Deque<Integer> que=new ArrayDeque<Integer>(); map[1]=1; que.offerLast(1); while(que.peekFirst()!=null){ int n=que.pollFirst(); int noo=Integer.bitCount(n); if(n-noo>=1&&map[n-noo]>map[n]+1){ map[n-noo]=map[n]+1; que.offerLast(n-noo); } if(n+noo<=t&&map[n+noo]>map[n]+1){ map[n+noo]=map[n]+1; que.offerLast(n+noo); } } System.out.println(map[t]==INF?-1:map[t]); } } }