-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocksequence1500.cpp
More file actions
36 lines (34 loc) · 828 Bytes
/
blocksequence1500.cpp
File metadata and controls
36 lines (34 loc) · 828 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
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<string>
using namespace std;
typedef long long ll;
int helper(int idx,vector<int> &nums,vector<int> &dp){
int n = nums.size();
if(idx >= n) return 0;
if(dp[idx] != -1) return dp[idx];
// remove requires one operation
int remove = 1 + helper(idx + 1,nums,dp);
// not remove
int notremove = INT_MAX;
if(idx + nums[idx] < n) notremove = helper(idx + nums[idx] + 1,nums,dp);
return dp[idx] = min(remove,notremove);
}
int main(){
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<int> nums(n);
for(int i = 0; i < n; i++){
cin >> nums[i];
}
vector<int> dp(n,-1);
cout << helper(0,nums,dp) << endl;
}
return 0;
}