Last active
May 29, 2019 17:28
-
-
Save harshraj22/5c5b2dc7c24ad8e6e59979730ffe8c16 to your computer and use it in GitHub Desktop.
getting WA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
#define ll long long int | |
/* | |
for each index, dp[0][i] stores length of longest increasing sequence starting at index i, and dp[1][i] stores length | |
of longest decreasing sequence starting at index i, | |
*/ | |
int main(){ | |
ios_base::sync_with_stdio(false); | |
cin.tie(0); cout.tie(0); | |
int test; | |
cin>>test; | |
while(test--){ | |
ll i,j,k=0,n,m; | |
cin>>n; | |
if(n<3){ | |
cout<<n<<"\n"; | |
continue; | |
} | |
vector<int> v(n);//represents the weight of car | |
for(i=0;i<n;i++) | |
cin>>v[i]; | |
ll dp[2][n]; | |
dp[0][n-1]=dp[1][n-1]=1; | |
for(i=n-2;i>=0;i--){ | |
dp[0][i]=dp[1][i]=1; | |
for(j=i+1;j<n;j++){ | |
if(v[i]<v[j] && dp[0][j]+1>dp[0][i]) | |
dp[0][i]=dp[0][j]+1; | |
else if(v[i]>v[j] && dp[1][j]+1>dp[1][i]) | |
dp[1][i]=dp[1][j]+1; | |
} | |
k=max(k,dp[0][i]+dp[1][i]-1); | |
} | |
cout<<k<<"\n"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment