fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. typedef long long ll;
  4.  
  5. using namespace std;
  6. const int N = 105;
  7.  
  8. int dp[N][N][N];
  9. int vis[N][N][N];
  10.  
  11. int n;
  12. int test_no;
  13. vector<int>a;
  14. int solve(int i, int l, int r){
  15. if(i == (n+1) ){
  16. if(l == 0 && r == n) return 0;
  17. else return 1e8;
  18. }
  19. if(vis[i][l][r] == test_no) return dp[i][l][r] ;
  20. vis[i][l][r] = test_no;
  21. int new_l = max(0, i - a[i]);
  22. new_l = min(l, new_l);
  23. int new_r = min(n, i + a[i] - 1);
  24. new_r = max(r, new_r);
  25. int op1 = 1 + solve(i+1, new_l, r);
  26. int op2 = 1 + solve(i+1, l, new_r);
  27. int op3;
  28. if(i > r && l == 0) op3 = solve(i+1,i,r);
  29. else op3 = solve(i+1,l,r);
  30. return dp[i][l][r] = min(op1, min(op2, op3));
  31.  
  32. }
  33. int main() {
  34. ios_base::sync_with_stdio(false);
  35. cin.tie(NULL); cout.tie(NULL);
  36. int t; cin>>t;
  37. for(int i = 1; i <= t; i++){
  38. test_no = i;
  39. cin>>n;
  40. a.clear(); a.resize(n+1);
  41. for(int i = 1 ; i<= n ; i++) cin>>a[i];
  42. cout<<solve(1,1,0)<<endl;
  43. }
  44. }
Success #stdin #stdout 0.01s 7796KB
stdin
13
1
1
2
1 1
2
2 1
2
1 2
2
2 2
3
1 1 1
3
3 1 2
3
1 3 1
7
1 2 3 1 2 4 2
7
2 1 1 1 2 3 1
10
2 2 5 1 6 1 8 2 8 2
6
2 1 2 1 1 2
6
1 1 4 1 3 2
stdout
100000000
2
2
2
2
3
3
2
3
6
2
5
3