-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockSequence.cpp
More file actions
41 lines (36 loc) · 882 Bytes
/
Copy pathBlockSequence.cpp
File metadata and controls
41 lines (36 loc) · 882 Bytes
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
// https://codeforces.com/problemset/problem/1881/E
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int INIT = -1, IMPOSSIBLE = 2e5+1;
// O(n).
int solve(int i, vector<int>& a, vector<int>& memo) {
if (i >= a.size()) {
return (i == a.size()) ? 0 : IMPOSSIBLE;
} else if (memo[i] != INIT) {
return memo[i];
}
int no_rm = solve(i+a[i]+1, a, memo);
int rm = 1+solve(i+1, a, memo);
return memo[i] = min(no_rm, rm);
}
void solveCase() {
int n;
cin >> n;
vector<int> a(n), memo(n, INIT);
for (int& a_i : a) {
cin >> a_i;
}
cout << solve(0, a, memo) << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int nC = 1;
cin >> nC;
for (int i = 1; i <= nC; i++) {
// cout << "\nTEST #" << i << ":\n";
solveCase();
}
return 0;
}