-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALGOA.cpp
48 lines (40 loc) · 1.1 KB
/
ALGOA.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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int min(int a, int b)
{
return a<b?a:b;
}
int main() {
int t;
scanf("%d",&t);
while(t--){
int i,j,k,n;
scanf("%d",&n);
int arr[n], dp[n+1];
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
dp[0] = 0;
for(i=1;i<n+1;i++)
{
dp[i] = dp[i-1]+1;
for(j=i-1;j>=0;j--){
if(j+arr[j] == i){
dp[i] = min(dp[i], 1+dp[j]);
}
}
}
for(i=1;i<n+1;i++)
{
dp[i] = dp[i-1]+1;
for(j=0;j<n;j++){
if(j+arr[j] == i){
dp[i] = min(dp[i], 1+dp[j]);
}
}
}
printf("%d\n",dp[n]);
}
}