fork download
  1. #include <bits/stdc++.h>
  2. typedef long long ll;
  3. using namespace std;
  4. ll T,n,a[110];
  5. ll dp[110][110][110];
  6. ll solve(ll ind,ll x,ll y)
  7. {
  8. if(ind == n + 1) return 0;
  9. if(dp[ind][x][y] != -1) return dp[ind][x][y];
  10. ll ret = solve(ind+1,x,y);
  11. if(x == 0) ret = max(ret,1 + solve(ind+1,ind,y)); // chose the 1st element belonging to the increasing seq.
  12. if(y == 0) ret = max(ret,1 + solve(ind+1,x,ind)); // chose the 1st element belonging to the decreasing seq.
  13. if(a[ind] > a[x]) ret = max(ret,1 + solve(ind+1,ind,y));
  14. if(a[ind] < a[y]) ret = max(ret,1 + solve(ind+1,x,ind));
  15. return dp[ind][x][y] = ret;
  16.  
  17. }
  18. int main()
  19. {
  20. cin >> T;
  21. while(T--)
  22. {
  23. memset(dp,-1,sizeof dp);
  24. cin >> n;
  25. for(ll i=1;i<=n;i++) cin >> a[i];
  26. cout << solve(1,0,0) << endl; // here solve(i , j , k) means we are at the ith index
  27. // 'j' means the last index we had chosen that was part of the INCREASING sequence
  28. // 'k' means the last index we had chosen that was part of the DECREASING sequence
  29. // j >= 0 and k >= 0 , but j == 0 means we have not yet chosen the first increasing sequence number
  30. // likewise k == 0 means the same
  31. }
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 4448KB
stdin
Standard input is empty
stdout
Standard output is empty