-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1746_Array_Description.cpp
More file actions
94 lines (73 loc) · 2.53 KB
/
1746_Array_Description.cpp
File metadata and controls
94 lines (73 loc) · 2.53 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <chrono>
#include <vector>
#include <algorithm>
using namespace std;
using namespace std::chrono;
const int MOD = 1e9 + 7;
int count_arrays_tab(int size, int upper_bound, vector<int> &arr)
{
vector<vector<int>> count_subarrays(size, vector<int>(upper_bound + 2));
for (int curr = 1; curr <= upper_bound; ++curr)
if (arr[0] == 0 || arr[0] == curr)
count_subarrays[0][curr] = 1;
for (int idx = 1; idx < size; ++idx)
for (int curr = 1; curr <= upper_bound; ++curr)
if (arr[idx] == 0 || arr[idx] == curr)
for (int diff = -1; diff <= 1; ++diff)
(count_subarrays[idx][curr] += count_subarrays[idx - 1][curr + diff]) %= MOD;
int total_count = 0;
for (int curr = 1; curr <= upper_bound; ++curr)
(total_count += count_subarrays[size - 1][curr]) %= MOD;
return total_count;
}
int count_subarrays_rec(int idx, int next, int upper_bound, vector<int> &arr, vector<vector<int>> &count_subarrays)
{
if (idx == -1)
return 1;
if (count_subarrays[idx][next] != -1)
return count_subarrays[idx][next];
int total_count = 0;
if (arr[idx] != 0)
{
if (abs(arr[idx] - next) > 1)
total_count = 0;
else
total_count = count_subarrays_rec(idx - 1, arr[idx], upper_bound, arr, count_subarrays);
}
else
for (int curr = max(1, next - 1); curr <= min(upper_bound, next + 1); ++curr)
(total_count += count_subarrays_rec(idx - 1, curr, upper_bound, arr, count_subarrays)) %= MOD;
return count_subarrays[idx][next] = total_count;
}
int count_arrays_rec(int size, int upper_bound, vector<int> &arr)
{
vector<vector<int>> count_subarrays(size, vector<int>(upper_bound + 1, -1));
if (arr[size - 1] != 0)
return count_subarrays_rec(size - 2, arr[size - 1], upper_bound, arr, count_subarrays);
int total_count = 0;
for (int curr = 1; curr <= upper_bound; ++curr)
(total_count += count_subarrays_rec(size - 2, curr, upper_bound, arr, count_subarrays)) %= MOD;
return total_count;
}
int count_arrays(int size, int upper_bound, vector<int> &arr)
{
if (size == 0)
return 0;
// return count_arrays_rec(size, upper_bound, arr);
return count_arrays_tab(size, upper_bound, arr);
}
int main()
{
int size, upper_bound;
cin >> size >> upper_bound;
vector<int> arr(size);
for (int i = 0; i < size; ++i)
cin >> arr[i];
auto start_time = high_resolution_clock::now();
cout << count_arrays(size, upper_bound, arr) << endl;
auto end_time = high_resolution_clock::now();
auto duration_ms = duration_cast<milliseconds>(end_time - start_time);
cerr << "Execution time: " << duration_ms.count() << " ms" << endl;
return 0;
}