-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpa2.cpp
59 lines (51 loc) · 937 Bytes
/
dpa2.cpp
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
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
template <class T>
inline bool chmax(T &a, T b)
{
if (a < b)
{
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
if (a > b)
{
a = b;
return 1;
}
return 0;
}
const long long INF = 1LL << 60;
// 入力
int N;
long long h[100010];
// DP テーブル
long long dp[100010];
int main()
{
int N;
cin >> N;
for (int i = 0; i < N; ++i)
cin >> h[i];
// 初期化 (最小化問題なので INF に初期化)
for (int i = 0; i < 100010; ++i)
dp[i] = 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;
}