fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. ios::sync_with_stdio(false);
  7. int n;
  8. cin >> n;
  9. vector<long long> v(n);
  10. for (int i = 0; i < n; ++i) {
  11. cin >> v[i];
  12. }
  13. int answer = 1;
  14. int dp[n][2];
  15.  
  16. for (int i = 0; i < n; ++i) {
  17. dp[i][0] = dp[i][1] = 1;
  18. }
  19. for (int i = 1; i < n; ++i) {
  20. // If positive
  21. if (v[i] > 0) {
  22. // If the previous element is negative
  23. // If previous element is 0, then 0
  24. if (v[i - 1] <= 0) {
  25. dp[i][1] += dp[i - 1][0];
  26. }
  27. } else if (v[i] < 0) {
  28. if (v[i - 1] >= 0) dp[i][0] += dp[i - 1][1];
  29. } else {
  30. // If the element is 0 and the previous element is negative
  31. // Then we consider this 0 as positive and update
  32. if (v[i - 1] < 0)
  33. dp[i][1] += dp[i - 1][0];
  34. else
  35. dp[i][0] += dp[i - 1][1];
  36. }
  37. answer = max(answer, dp[i][0]);
  38. answer = max(answer, dp[i][1]);
  39. }
  40. cout << answer << '\n';
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5508KB
stdin
11
6 2 -2 0 3 3 -2 -2 0 -100000 4
stdout
4