結果

問題 No.3 ビットすごろく
ユーザー uafr_csuafr_cs
提出日時 2015-05-29 01:02:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 140 ms / 5,000 ms
コード長 935 bytes
コンパイル時間 2,069 ms
コンパイル使用メモリ 74,748 KB
実行使用メモリ 56,276 KB
最終ジャッジ日時 2023-09-13 23:11:59
合計ジャッジ時間 7,729 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,520 KB
testcase_01 AC 126 ms
56,024 KB
testcase_02 AC 127 ms
55,928 KB
testcase_03 AC 129 ms
55,524 KB
testcase_04 AC 126 ms
56,276 KB
testcase_05 AC 130 ms
56,096 KB
testcase_06 AC 128 ms
55,940 KB
testcase_07 AC 127 ms
56,144 KB
testcase_08 AC 129 ms
55,968 KB
testcase_09 AC 131 ms
55,712 KB
testcase_10 AC 134 ms
55,740 KB
testcase_11 AC 128 ms
55,464 KB
testcase_12 AC 129 ms
55,724 KB
testcase_13 AC 127 ms
56,256 KB
testcase_14 AC 135 ms
56,092 KB
testcase_15 AC 134 ms
55,844 KB
testcase_16 AC 140 ms
55,788 KB
testcase_17 AC 134 ms
56,208 KB
testcase_18 AC 128 ms
56,240 KB
testcase_19 AC 139 ms
56,188 KB
testcase_20 AC 129 ms
55,800 KB
testcase_21 AC 127 ms
55,896 KB
testcase_22 AC 137 ms
56,112 KB
testcase_23 AC 136 ms
56,076 KB
testcase_24 AC 135 ms
55,820 KB
testcase_25 AC 134 ms
55,848 KB
testcase_26 AC 124 ms
55,948 KB
testcase_27 AC 128 ms
55,776 KB
testcase_28 AC 132 ms
56,032 KB
testcase_29 AC 133 ms
55,808 KB
testcase_30 AC 124 ms
56,032 KB
testcase_31 AC 125 ms
56,128 KB
testcase_32 AC 129 ms
56,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		
		int[] min_costs = new int[N + 1];
		Arrays.fill(min_costs, Integer.MAX_VALUE);
		min_costs[1] = 1;
		
		Queue<Integer> queue = new LinkedList<Integer>();
		queue.add(1);
		
		while(!queue.isEmpty()){
			final int pos = queue.poll();
			final int pops = Integer.bitCount(pos);
			
			
			for(int move : new int[]{-pops, pops}){
				final int next_pos = pos + move;
				
				if(next_pos < 1 || next_pos > N){
					continue;
				}else if(min_costs[next_pos] <= min_costs[pos] + 1){
					continue;
				}else{
					min_costs[next_pos] = min_costs[pos] + 1;
					queue.add(next_pos);
				}
				
			}
		}
		
		System.out.println(min_costs[N] == Integer.MAX_VALUE ? -1 : min_costs[N]);
		
		
	}
	
}
0