CNeed help in converting dp solution to a recursive one
Difference between en1 and en2, changed 18 character(s)
I am in my early stages of learning dp <br>↵
Just wanted to know is it always possible to convert a dp solution into a recursive one<br>↵
If so can anyone write the recursive code for this dp solution<br>↵

<spoiler summary="Spoiler">↵
...   ↵
  #include <bits/stdc++.h>↵

  #define lli long long int↵

  #define pii pair<int,int>↵

  #define mii map<int,int>↵

  #define vi vector<int> ↵
                     ↵
  #define pb push_back    ↵
                   ↵
  #define vlli vector<long long int>↵

  #define nl cout<<'\n'↵

  #define yy cout << "YES\n" ↵

  #define nn cout << "NO\n" ↵

  #define all(a) a.begin(),a.end()↵

  #define IM  INT_MAX↵

  #define IMN INT_MIN↵

  #define pc cout<<count<<'\n'↵

  #define int long long int↵

  #define mp make_pair↵

  #define vpii vector<pair<int,int>>↵

  #define Yy cout<<"Yes\n"↵

  #define Nn cout<<"No\n"↵

  using namespace std;↵

  signed main(){↵

     ios_base::sync_with_stdio(false);↵

      cin.tie(NULL);↵

     int t;cin>>t;↵

     while(t--){↵

       int n;cin>>n;↵

        vi v;↵

        v.pb(0);↵

        for(int i=1;i<=n;i++){↵

            int x;cin>>x;↵

            v.pb(x);↵

        }↵

        vector<int>dp(n+1,LLONG_MAX);↵

        dp[0]=0; ↵

        for(int i=1;i<=n;i++){↵

            dp[i]=min(dp[i-1]+1,dp[i]);↵

            if(i+v[i]<=n){↵

                dp[i+v[i]]=min(dp[i+v[i]],dp[i-1]);↵

            }↵

            if(i-v[i]>=1)dp[i]=min(dp[i],dp[i-v[i]-1]);↵

        }↵

        cout<<dp[n];↵

        nl;↵

     } ↵

  }↵

</spoiler>↵

<br>↵
The question is similar to [this](https://codeforces.com/problemset/problem/1741/E), the only difference is that we have to find the minimum elements to remove from the sequence b so that it can be sent over the network

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English ayush118 2024-06-28 20:22:48 18
en1 English ayush118 2024-06-28 20:17:15 1838 Initial revision (published)