forked from drken1215/book_algorithm_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_5_3.cpp
More file actions
35 lines (28 loc) · 695 Bytes
/
code_5_3.cpp
File metadata and controls
35 lines (28 loc) · 695 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
template<class T> void chmin(T& a, T b) {
if (a > b) {
a = b;
}
}
const long long INF = 1LL << 60; // 十分大きい値とする (ここでは 2^60)
int main() {
// 入力
int N; cin >> N;
vector<long long> h(N);
for (int i = 0; i < N; ++i) cin >> h[i];
// 初期化 (最小化問題なので INF に初期化)
vector<long long> dp(N, INF);
// 初期条件
dp[0] = 0;
// ループ
for (int i = 1; i < N; ++i) {
chmin(dp[i], dp[i - 1] + abs(h[i] - h[i - 1]));
if (i > 1) {
chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
}
}
// 答え
cout << dp[N - 1] << endl;
}