結果
| 問題 |
No.1868 Teleporting Cyanmond
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2024-07-29 18:10:34 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 273 ms / 2,000 ms |
| コード長 | 2,046 bytes |
| コンパイル時間 | 2,772 ms |
| コンパイル使用メモリ | 79,688 KB |
| 実行使用メモリ | 58,508 KB |
| 最終ジャッジ日時 | 2024-07-29 18:10:45 |
| 合計ジャッジ時間 | 10,565 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
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[] rights = new int[n - 1];
for (int i = 0; i < n - 1; i++) {
rights[i] = sc.nextInt() - 1;
}
Deque<Path> deq = new ArrayDeque<>();
deq.add(new Path(0, 0));
int[] costs = new int[n];
Arrays.fill(costs, Integer.MAX_VALUE);
while (deq.size() > 0) {
Path p = deq.poll();
if (costs[p.idx] <= p.value) {
continue;
}
costs[p.idx] = p.value;
if (p.idx > 0) {
deq.addFirst(new Path(p.idx - 1, p.value));
}
if (p.idx < n - 1) {
deq.addLast(new Path(rights[p.idx], p.value + 1));
}
}
System.out.println(costs[n - 1]);
}
static class Path {
int idx;
int value;
public Path(int idx, int value) {
this.idx = idx;
this.value = value;
}
}
}
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