Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. n = int(input())
  2. a = list(map(int, input().split()))
  3. if n <= 1:
  4.     print('0')
  5.     raise SystemExit(0)
  6. i = 1
  7. while i < n and a[i - 1] == a[i]:
  8.     i += 1
  9. if i == n:
  10.     print(n - 1)
  11.     raise SystemExit(0)
  12. ans = i - 1
  13. if a[i - 1] < a[i]:
  14.     er = 1
  15. else:
  16.     er = 2
  17. while i < n:
  18.     j = i + 1
  19.     if er == 1:
  20.         while j < n and a[j - 1] <= a[j]:
  21.             j += 1
  22.         ans += j - i - 1
  23.         er = 2
  24.     else:
  25.         while j < n and a[j - 1] >= a[j]:
  26.             j += 1
  27.         ans += j - i - 1
  28.         er = 1
  29.     i = j
  30. print(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement