結果

問題 No.3 ビットすごろく
ユーザー villachvillach
提出日時 2017-09-19 00:47:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 275 ms / 5,000 ms
コード長 1,052 bytes
コンパイル時間 3,500 ms
コンパイル使用メモリ 80,320 KB
実行使用メモリ 56,560 KB
最終ジャッジ日時 2023-09-14 00:49:32
合計ジャッジ時間 10,697 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
56,064 KB
testcase_01 AC 122 ms
56,104 KB
testcase_02 AC 118 ms
56,140 KB
testcase_03 AC 147 ms
55,692 KB
testcase_04 AC 140 ms
55,544 KB
testcase_05 AC 187 ms
55,724 KB
testcase_06 AC 143 ms
55,848 KB
testcase_07 AC 135 ms
55,868 KB
testcase_08 AC 157 ms
53,488 KB
testcase_09 AC 198 ms
56,088 KB
testcase_10 AC 233 ms
55,724 KB
testcase_11 AC 189 ms
55,852 KB
testcase_12 AC 174 ms
56,560 KB
testcase_13 AC 137 ms
55,952 KB
testcase_14 AC 225 ms
55,596 KB
testcase_15 AC 264 ms
56,068 KB
testcase_16 AC 250 ms
56,060 KB
testcase_17 AC 261 ms
55,792 KB
testcase_18 AC 133 ms
56,060 KB
testcase_19 AC 263 ms
55,996 KB
testcase_20 AC 126 ms
56,376 KB
testcase_21 AC 112 ms
56,064 KB
testcase_22 AC 227 ms
56,108 KB
testcase_23 AC 267 ms
56,548 KB
testcase_24 AC 275 ms
55,696 KB
testcase_25 AC 269 ms
55,884 KB
testcase_26 AC 117 ms
56,192 KB
testcase_27 AC 134 ms
55,844 KB
testcase_28 AC 244 ms
56,380 KB
testcase_29 AC 190 ms
54,400 KB
testcase_30 AC 118 ms
56,064 KB
testcase_31 AC 122 ms
56,296 KB
testcase_32 AC 185 ms
55,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.LinkedList;
import java.util.Scanner;

public class N3 {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		LinkedList<Integer> queue = new LinkedList<Integer>();
		LinkedList<Integer> visited = new LinkedList<Integer>();
		int i = 1;
		int a, b, dist;
		
		for(int j = 0;j <= n;++j){
			visited.add(0);
		}
		
		if(n == 1){
			System.out.println(1);
			return;
		}
		
		while(true){
			dist = visited.get(i);
			a = i - countBin(i);
			b = i + countBin(i);
			if(a > 0 && a <= n && visited.get(a) == 0){
				visited.set(a, dist + 1);
				queue.offer(a);
			}
			if(b > 0 && b <= n && visited.get(b) == 0){
				visited.set(b, dist + 1);
				queue.offer(b);
			}
			
			if(a == n || b == n){
				System.out.println(dist + 2);
				return;
			}
			
			if(queue.size() == 0){
				System.out.println(-1);
				return;
			}
			i = queue.poll();
		}
	}
	
	public static int countBin(int x){
		int i = 0;
		while(x != 0){
			i += x % 2;
			x /= 2;
		}
		return i;
	}
}
0