-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path1013E. Hills.cpp
65 lines (55 loc) · 1.32 KB
/
1013E. Hills.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
60
61
62
63
64
65
/*
Idea:
- Dynamic programming.
- We can start dynamic programming `k` times and in each
step `i` we try either to make the current house higher
than its neighbors and move to `i + 2` or leave it and
move to `i + 1`.
*/
#include <bits/stdc++.h>
using namespace std;
int const N = 5e3 + 100;
int n, a[N];
int dp[N][N][2];
int rec(int idx, int rem, bool lst) {
if(rem == 0)
return 0;
if(idx > n)
return 1e9;
int &ret = dp[idx][rem][lst];
if(ret != -1)
return ret;
ret = rec(idx + 1, rem, 0);
int add = 0;
if(lst) {
int tmp = a[idx - 1];
if(a[idx - 1] >= a[idx - 2]) {
tmp = a[idx - 1] - a[idx - 2] + 1;
tmp = a[idx - 1] - tmp;
}
if(tmp >= a[idx])
add = tmp - a[idx] + 1;
if(a[idx + 1] >= a[idx])
add += a[idx + 1] - a[idx] + 1;
ret = min(ret, rec(idx + 2, rem - 1, 1) + add);
} else {
if(a[idx - 1] >= a[idx])
add = a[idx - 1] - a[idx] + 1;
if(a[idx + 1] >= a[idx])
add += a[idx + 1] - a[idx] + 1;
ret = min(ret, rec(idx + 2, rem - 1, 1) + add);
}
return ret;
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
scanf("%d", a + i);
a[0] = -1e9;
a[n + 1] = -1e9;
memset(dp, -1, sizeof dp);
for(int i = 1; i <= (n + 1) / 2; ++i)
printf("%d ", rec(1, i, 0));
puts("");
return 0;
}