-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB17.js
More file actions
83 lines (61 loc) · 1.6 KB
/
Copy pathB17.js
File metadata and controls
83 lines (61 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// * `fs` を使って標準入力を読み込み、
// * 最小コスト経路の復元付きで出力し、
// * 実行時間・メモリ効率に注意した構成です。
// ---
// ### ✅ コード(`index.js`)
// ```javascript
const fs = require("fs");
const input = fs.readFileSync("/dev/stdin", "utf8").trim().split("\n");
const N = parseInt(input[0]);
const h = input[1].split(" ").map(Number);
const dp = new Array(N).fill(Infinity);
const prev = new Array(N).fill(-1);
// 初期化
dp[0] = 0;
for (let i = 1; i < N; i++) {
const cost1 = dp[i - 1] + Math.abs(h[i] - h[i - 1]);
if (cost1 < dp[i]) {
dp[i] = cost1;
prev[i] = i - 1;
}
if (i > 1) {
const cost2 = dp[i - 2] + Math.abs(h[i] - h[i - 2]);
if (cost2 < dp[i]) {
dp[i] = cost2;
prev[i] = i - 2;
}
}
}
// 経路復元
const path = [];
let current = N - 1;
while (current !== -1) {
path.push(current + 1); // 足場番号に変換(1-indexed)
current = prev[current];
}
path.reverse();
// 出力
console.log(path.length);
console.log(path.join(" "));
// ### 💡 解説
// * `dp[i]`: 足場 `i+1` に到達する最小コスト。
// * `prev[i]`: 最小コストで `i` に来たときの直前の足場インデックス。
// * 計算量:`O(N)`
// * メモリ:`O(N)`(配列2本)
// ---
// ### 🧪 入力例1の動作確認
// 入力:
// ```
// 4
// 10 30 40 20
// ```
// 出力(例の1つ):
// ```
// 3
// 1 2 4
// ```
// ---
// ### ⏱️ 実行時間・メモリ効率
// * `N = 100000` でも高速(`O(N)`)
// * 使用メモリ:`~1MB`程度(`dp`, `prev` 各100,000)
// ---