fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const long long MaxN = 5e5 + 5;
  4. long long n,a[MaxN],dp[MaxN][2];
  5. int main()
  6. {
  7. cin >> n;
  8. for (long long i=1; i<=n; i++)
  9. {
  10. cin >> a[i];
  11. }
  12. long long res=0;
  13. for (long long i=1; i<=n; i++)
  14. {
  15. dp[a[i]][0]=1;
  16. dp[a[i]][0]=max(dp[a[i]-1][0]+1,dp[a[i]][0]);
  17. if(a[i]-2>=0)
  18. {
  19. dp[a[i]][1]=max(dp[a[i]-2][0]+2,dp[a[i]][1]);
  20. }
  21. dp[a[i]][1]=max(dp[a[i]-1][1]+1,dp[a[i]][1]);
  22. res=max({res,dp[a[i]][0],dp[a[i]][1]});
  23. }
  24. cout << res;
  25. }
  26.  
Success #stdin #stdout 0s 5300KB
stdin
Standard input is empty
stdout
Standard output is empty