結果

問題 No.3 ビットすごろく
ユーザー core123core123
提出日時 2019-04-17 13:48:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 135 ms / 5,000 ms
コード長 1,395 bytes
コンパイル時間 2,374 ms
コンパイル使用メモリ 81,252 KB
実行使用メモリ 56,348 KB
最終ジャッジ日時 2023-09-14 01:16:09
合計ジャッジ時間 8,502 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,964 KB
testcase_01 AC 125 ms
56,056 KB
testcase_02 AC 123 ms
55,904 KB
testcase_03 AC 129 ms
55,732 KB
testcase_04 AC 125 ms
55,816 KB
testcase_05 AC 134 ms
55,984 KB
testcase_06 AC 127 ms
55,800 KB
testcase_07 AC 128 ms
55,900 KB
testcase_08 AC 135 ms
55,740 KB
testcase_09 AC 131 ms
55,644 KB
testcase_10 AC 132 ms
55,836 KB
testcase_11 AC 131 ms
56,056 KB
testcase_12 AC 132 ms
55,476 KB
testcase_13 AC 127 ms
55,976 KB
testcase_14 AC 132 ms
55,736 KB
testcase_15 AC 133 ms
55,880 KB
testcase_16 AC 133 ms
56,060 KB
testcase_17 AC 133 ms
55,800 KB
testcase_18 AC 127 ms
55,844 KB
testcase_19 AC 135 ms
55,948 KB
testcase_20 AC 129 ms
56,348 KB
testcase_21 AC 124 ms
55,728 KB
testcase_22 AC 135 ms
55,740 KB
testcase_23 AC 134 ms
55,812 KB
testcase_24 AC 132 ms
55,700 KB
testcase_25 AC 133 ms
55,720 KB
testcase_26 AC 123 ms
55,952 KB
testcase_27 AC 126 ms
56,232 KB
testcase_28 AC 133 ms
55,712 KB
testcase_29 AC 134 ms
56,116 KB
testcase_30 AC 125 ms
55,788 KB
testcase_31 AC 126 ms
55,800 KB
testcase_32 AC 134 ms
55,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.util.stream.*;
public class Main{
	public static void main(String... args){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int[] ans=new int[n];
		Arrays.fill(ans,Integer.MAX_VALUE);
		Deque<Pair> deq=new ArrayDeque<>();
		deq.add(new Pair(0,0));
		while(deq.size()>0){
			Pair p=deq.removeFirst();
			//put(p);
			//put(Arrays.toString(ans));
			if(ans[p.x]>p.y){
				ans[p.x]=p.y;
				int index=p.x-Integer.bitCount(p.x+1);
				if(index>=0&&index<n)deq.addLast(new Pair(index,p.y+1));
				index=p.x+Integer.bitCount(p.x+1);
				if(index>=0&&index<n)deq.addLast(new Pair(index,p.y+1));
			}
		}
		if(ans[n-1]==Integer.MAX_VALUE){
			put(-1);
		}else{
			put(ans[n-1]+1);
		}
		
	}
	public static class Pair{
		final int x,y;
		Pair(int x,int y){
			this.x=x;
			this.y=y;
		}
		@Override
		public String toString(){
			return String.format("Pair(%d,%d)",x,y);
		}
	}
	
	public static void print(Object object){
        System.out.print(object);
    }
    public static void put(Object object) {
        System.out.println(object);
    }
    public static void put(){
        System.out.println();
    }
 
    public static void print(String format,Object... args){
        System.out.print(String.format(format,args));
    }
    public static void put(String format,Object... args) {
        System.out.println(String.format(format,args));
    }
}
0