Skip to content

Commit

Permalink
Longest Bitnoic Subsequence
Browse files Browse the repository at this point in the history
  • Loading branch information
fnf47 authored Mar 6, 2017
1 parent fe75f90 commit def428e
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions DP/Longest_Bitonic_Subsequence/Longest_Bitonic_Subsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@


// Longest Bitonic Subsequence
// O(n*n)
// Tested on SPOJ CODERE3

#include <bits/stdc++.h>
#define endl '\n';
using namespace std;
typedef long long int LL;

const int N=1003;
int a[N],dpl[N],dpr[N];

int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);

//freopen("input.in","r",stdin);

int t;cin>>t;while(t--) // Number of testcases
{
int n; cin>>n;

for(int i=1;i<=n;i++)cin>>a[i];

for(int i=1;i<=n;i++)
{
dpl[i]=1;
for(int j=1;j<=i-1;j++)
{
if(a[j]<a[i])dpl[i]=max(dpl[i],1+dpl[j]);
}
}

for(int i=n;i>=1;i--)
{
dpr[i]=1;
for(int j=i+1;j<=n;j++)
{
if(a[j]<a[i])dpr[i]=max(dpr[i],1+dpr[j]);
}
}

int res=0;

for(int i=1;i<=n;i++)
{
int tmp=max(dpl[i],dpr[i]);
tmp=max(tmp,dpl[i]+dpr[i]-1);
res=max(res,tmp);
}

cout<<res<<endl;
}

return 0;
}

0 comments on commit def428e

Please sign in to comment.