結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
core123
|
| 提出日時 | 2019-04-17 13:48:42 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 143 ms / 5,000 ms |
| コード長 | 1,395 bytes |
| コンパイル時間 | 2,323 ms |
| コンパイル使用メモリ | 79,284 KB |
| 実行使用メモリ | 42,188 KB |
| 最終ジャッジ日時 | 2024-07-01 09:18:09 |
| 合計ジャッジ時間 | 7,854 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
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));
}
}
core123