BAT3 - BATMAN3 - SPOJ Solution C++

  Problem Link : BAT3 


👉 Hint : Simple 1D DP

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
int dp[1001];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
    {
        int n,m,ans=0;
        cin>>n>>m;
        int arr[n];
        for(int i=0;i<n;i++)
            cin>>arr[i];
        dp[n-1]=1;
        for(int i=n-2;i>=0;i--)
        {
            dp[i]=1;
            for(int j=i+1;j<n;j++)
            {
                if(arr[j]<arr[i])
                    dp[i]=max(dp[i],1+dp[j]);
            }
            if(i==m)
            {
                for(int j=i+1;j<n;j++)
                {
                if(arr[j]>=arr[i])
                    dp[i]=max(dp[i],1+dp[j]);
                }
            }
            if(dp[i]>ans)
                ans=dp[i];
 
        }
        cout<<ans<<endl;
 
    }
} 

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!