-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayDescription.cpp
More file actions
63 lines (57 loc) · 1.41 KB
/
ArrayDescription.cpp
File metadata and controls
63 lines (57 loc) · 1.41 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
//Array Description - https://cses.fi/problemset/task/1746
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
void solve() {
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<ll>> dp(n + 1, vector<ll>(m + 1, -1));
function<ll(int, int)> dfs = [&](int index, int last) -> ll {
if (index >= n) {
return 1;
}
if (last != 0 && a[index] != 0 && abs(a[index] - last) > 1) {
return 0;
}
if (dp[index][last] != -1) {
return dp[index][last];
}
if (a[index] != 0) {
return dfs(index + 1, a[index]);
}
ll ans = 0;
if (last != 0) {
if (last < m) {
ans += dfs(index + 1, last + 1);
}
if (last > 1) {
ans += dfs(index + 1, last - 1);
}
ans += dfs(index + 1, last);
ans %= MOD;
} else {
for (int x = 1; x <= m; x++) {
ans += dfs(index + 1, x);
ans %= MOD;
}
}
return dp[index][last] = ans;
};
cout << dfs(0, 0) << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}