-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA - Frog 1.cpp
More file actions
68 lines (58 loc) · 1.23 KB
/
Copy pathA - Frog 1.cpp
File metadata and controls
68 lines (58 loc) · 1.23 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
/**
* author: _Berlin_
* created: 04.04.2024 10:31:51 AM
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef BERLIN
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
const int N = 1e5 + 9;
const int inf = 1e9;
int n, h[N], dp[N];
int frog(int i) {
if(i > n) return inf;
if(i == n) return 0;
int &ans = dp[i];
if(ans != -1) return ans;
ans = abs(h[i] - h[i + 1]) + frog(i + 1);
ans = min(ans, abs(h[i] - h[i + 2]) + frog(i + 2));
return ans;
}
int ffrog(int i) {
if(i < 0) return inf;
if(i == 1) return 0;
int &ans = dp[i];
if(ans != -1) return ans;
ans = abs(h[i] - h[i - 1]) + ffrog(i - 1);
ans = min(ans, abs(h[i] - h[i - 2]) + ffrog(i - 2));
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> h[i];
}
memset(dp, -1, sizeof dp);
// Top-Down Approach
cout << frog(1) << "\n";
cout << ffrog(n) << "\n";
// Bottom-Up Approach
for(int i = n; i >= 1; i--) {
if(i == n) {
dp[i] = 0;
}else {
int &ans = dp[i];
ans = abs(h[i] - h[i + 1]) + dp[i + 1];
if(i + 2 <= n) {
ans = min(ans, abs(h[i] - h[i + 2]) + dp[i + 2]);
}
}
}
cout << dp[1] << "\n";
return 0;
}