結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2024-02-05 13:25:24 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 78 ms / 5,000 ms |
| コード長 | 2,140 bytes |
| コンパイル時間 | 4,525 ms |
| コンパイル使用メモリ | 79,540 KB |
| 実行使用メモリ | 37,576 KB |
| 最終ジャッジ日時 | 2024-09-28 11:33:35 |
| 合計ジャッジ時間 | 7,705 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int[] dp = new int[n + 1];
ArrayDeque<Path> deq = new ArrayDeque<>();
deq.add(new Path(1, 1));
Arrays.fill(dp, Integer.MAX_VALUE);
while (deq.size() > 0) {
Path p = deq.poll();
if (dp[p.idx] <= p.value) {
continue;
}
dp[p.idx] = p.value;
int pop = getPop(p.idx);
if (p.idx + pop <= n) {
deq.add(new Path(p.idx + pop, p.value + 1));
}
if (p.idx - pop >= 1) {
deq.add(new Path(p.idx - pop, p.value + 1));
}
}
System.out.println(dp[n] == Integer.MAX_VALUE ? -1 : dp[n]);
}
static class Path {
int idx;
int value;
public Path(int idx, int value) {
this.idx = idx;
this.value = value;
}
}
static int getPop(int x) {
int pop = 0;
while (x > 0) {
pop += x % 2;
x >>= 1;
}
return pop;
}
}
class Scanner {
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
try {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return st.nextToken();
}
}
}
tenten