Problem:
 Score : 300 points    Problem Statement You are given an array A of length N .
Your task is to divide it into several contiguous subarrays.
Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order.
At least how many subarrays do you need to divide A into?      Constraints  1 \leq N \leq 10^5  1 \leq A_i \leq 10^9  Each A_i is an integer.         Input Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_N       Output Print the minimum possible number of subarrays after division of A .        Sample Input 1 6
1 2 3 2 2 1      Sample Output 1 2  One optimal solution is to divide the array into [1,2,3] and [2,2,1] .       Sample Input 2 9
1 2 1 2 1 2 1 2 1      Sample Output 2 5       Sample Input 3 7
1 2 3 2 1 999999999 1000000000      Sample Output 3 3

Solution (Python):
import sys
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
flg = None 
cur = a_ls[0]
cnt = 1
for a in a_ls[1:]:
    if a > cur:
        if flg is None:
            flg = True
        if not flg:
            flg = None
            cnt += 1
    if a < cur:
        if flg is None:
            flg = False
        if flg:
            cnt += 1
            flg = None
    cur = a
print(cnt)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

ans = 1
trend = 0
last = -1
for a in A:
    if last == -1:
        last = a
        continue
    if a > last:
        if trend < 0:
            ans += 1
            last = -1
            trend = 0
        else:
            trend = 1
    elif a < last:
        if trend > 0:
            ans += 1
            last = -1
            trend = 0
        else:
            trend = -1
    last = a

print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split(' ')))
A_short = [A[0]]
tmp = A[0]
ans = 1
cnt = 0
for i in A[1:]:
    if tmp != i:
        A_short.append(i)
    tmp = i
while cnt < len(A_short)-2:
    if A_short[cnt] >= A_short[cnt+1] and A_short[cnt+1] <A_short[cnt+2]:
        cnt += 1
        ans += 1
    elif A_short[cnt] <= A_short[cnt+1] and A_short[cnt+1] > A_short[cnt+2]:
        cnt += 1
        ans += 1
    cnt += 1
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, *A = map(int, read().split())

cnt = 1
flag = 'E'
for i, a in enumerate(A):
    if i == N - 1:
        break

    if a < A[i + 1]:
        if flag == 'D':
            cnt += 1
            flag = 'E'
        else:
            flag = 'U'
    elif a > A[i + 1]:
        if flag == 'U':
            cnt += 1
            flag = 'E'
        else:
            flag = 'D'

print(cnt)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = 1
flag = 0
for i in range(N-1):
    if A[i+1] < A[i] and flag == 0:
        flag = 1 
    elif A[i+1] > A[i] and flag ==0:
        flag = 2 
    elif flag == 1 and (A[i+1] > A[i]):
        ans += 1 
        flag = 0 
    elif flag == 2 and (A[i+1] < A[i]):
        ans += 1
        flag = 0
    
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

count = 1
state = 0
for i in range(N-1):
    diff = A[i+1] - A[i]
    if state == 0:
        if diff > 0:
            state = 1
            continue
        if diff == 0:
            state = 0
            continue
        if diff < 0:
            state = -1
            continue
    if state == 1:
        if diff > 0:
            state = 1
            continue
        if diff == 0:
            state = 1
            continue
        if diff < 0:
            count += 1
            state = 0
            continue
    if state == -1:
        if diff > 0:
            count += 1
            state = 0
            continue
        if diff == 0:
            state = -1
            continue
        if diff < 0:
            state = -1
            continue

print(count)

Solution (Python):

n = int(input())
arr = list(map(int, input().split()))
ans = 1
state = 0  
for i in range(n - 1):
    if state == 0:
        if arr[i] > arr[i + 1]:
            state = -1
        elif arr[i] < arr[i + 1]:
            state = 1
    elif state == 1:
        if arr[i] < arr[i + 1]:
            continue
        elif arr[i] > arr[i + 1]:
            
            state = 0
            ans += 1
        else:
            continue
    elif state == -1:
        if arr[i] > arr[i + 1]:
            continue
        elif arr[i] < arr[i + 1]:
            
            state = 0
            ans += 1
        else:
            continue

print(ans)

Solution (Python):
n = int(input())
al = list(map(int,input().split()))

ans = 1
mode = "center"
now = al[0]
for a in al:
    if mode == "center":
        if a > now:
            mode = "up"
            now = a
        elif a < now:
            mode = "down"
            now = a
    elif mode == "up":
        if a > now:
            now = a
        elif a < now:
            now = a
            mode = "center"
            ans += 1
    elif mode == "down":
        if a > now:
            now = a
            mode = "center"
            ans += 1
        elif a < now:
            now = a
print(ans)

Solution (Python):
N = int(input())
input_array = list(map(int, input().split()))

upper = False
lower = False
res = 1
for i in range(len(input_array) - 1):
    if input_array[i] == input_array[i + 1]:
        continue
    elif input_array[i] < input_array[i + 1]:
        upper = True
    else:
        lower = True

    if upper is True and lower is True:
        res += 1
        upper = False
        lower = False

print(res)

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
cnt = 1
flag = 0 
for i in range(1,N):
    if P[i-1]>P[i] and flag == 0:
        flag = 1
    elif P[i-1]<P[i] and flag == 0:
        flag = 2
    elif flag == 1 and (P[i-1] < P[i]) :
        cnt += 1
        flag = 0
    elif flag == 2 and (P[i-1] > P[i]) :
        cnt += 1
        flag = 0
print(cnt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

answer = 0
up_or_down = 0
previous = None

for i in a:
    if previous is None or previous == i:
        pass
    elif previous < i:
        if up_or_down == -1:
            answer += 1
            up_or_down = 0
        else:
            up_or_down = 1
    elif previous > i:
        if up_or_down == 1:
            answer += 1
            up_or_down = 0
        else:
            up_or_down = -1
    previous = i

print(answer + 1)

Solution (Python):
from sys import stdin

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))+[0]
li = [li[i] for i in range(len(li)-1) if li[i] != li[i+1]]

if n <= 2:
    print(1)
    exit()

count = 1
flag = 0
for i in range(1,len(li)-1):
    if flag == 1:
        flag = 0
        continue
    else:
        if li[i-1] > li[i] < li[i+1] or li[i-1] < li[i] > li[i+1]:
            flag = 1
            count += 1
print(count)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
last = a[0]
direction = 0
ans = 1


for a in a[1:]:
    if direction == 0:
        if last < a:
            direction = 1
        elif last > a:
            direction = -1
        last = a
        continue
    if direction == 1:
        if last <= a:
            last = a
            continue
        else:
            last = a
            direction = 0
            ans += 1
            continue
    if direction == -1:
        if last >= a:
            last = a
            continue
        else:
            last = a
            direction = 0
            ans += 1
            continue
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

count = 0
multi = 1

while len(A) > 0:
    i = 0
    sign = A[min(1, len(A)-1)] - A[0]
    multi = 0
    if len(A) != 1:
        while multi >= 0:
            if len(A) <= i+2:
                i = len(A)-1
                break
            else:
                i += 1
                multi = (A[i+1] - A[i]) * sign
                if sign == 0:
                    sign = (A[i+1]-A[i])
        del A[0:i+1]
        count += 1
    else:
        del A[0]
        count += 1

print(count)

Solution (Python):
n = int(input())
arr = list(map(int,input().split()))

before = ud = None
cutCnt = 0
i = 0
while i < n:
  if before is None:
    before = arr[i]
    continue
  else:
    if ud is None:
      if before > arr[i]:
        ud = 1
        before = arr[i]
      elif before < arr[i]:
        ud = 2
        before = arr[i]
    elif ud == 1 and before < arr[i]:
      cutCnt+=1
      ud = None
      before = None
      i-=1
    elif ud == 2 and before > arr[i]:
      cutCnt+=1
      ud = None
      before = None
      i-=1
    else:
      before =arr[i]
    i+=1
    
print(cutCnt+1)

Solution (Python):
N = int(input())

A = list(map(int,input().split()))

cnt = 0
mode = 0

for i in range(len(A)):
    if i == 0:
        continue
    
    if (A[i] - A[i-1] >= 0 and mode >=0) or (A[i] - A[i-1] <= 0 and mode <= 0):
        if A[i] - A[i-1] > 0:
            mode = 1
        if A[i] - A[i-1] < 0:
            mode = -1
    else:
        mode = 0
        cnt += 1

print(cnt+1)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

cond = 0
tmp = A[0]
cnt = 0
for i in range(1,N):
	if cond == 0:
		if A[i] == tmp:
			tmp = A[i]
			continue
		elif A[i] < tmp:
			tmp = A[i]
			cond = -1
			continue
		else:
			tmp = A[i]
			cond = 1
			continue

	elif cond == -1:
		if A[i] > tmp:
			cnt += 1
			cond = 0
			tmp = A[i]
			continue
		else:
			tmp = A[i]
			continue

	else:
		if A[i] < tmp:
			cnt += 1
			cond = 0
			tmp = A[i]
			continue
		else:
			tmp = A[i]
			continue

print(cnt + 1)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = 1
state = 0
for i in range(N-1):
    
    if state == 1:
        if A[i] > A[i+1]:
            ans += 1
            state = 0
    elif state == -1:
        if A[i] < A[i+1]:
            ans += 1
            state = 0
    elif state == 0:
        if A[i] < A[i+1]:
            state = 1
        elif A[i] > A[i+1]:
            state = -1
print(ans)

Solution (Python):
N=int(input())
li = list(map(int,input().split()))

x = ""
count = 1

for i in range(N - 1):
  if li[i] == li[i + 1]:
    continue

  if x == "":
    if li[i] < li[i + 1]:
      x = 'UP'
    
    if li[i] > li[i + 1]:
      x = 'DOWN'

  else:
    if x == 'UP' and li[i] > li[i + 1]:
      x = ""
      count += 1
    elif x == 'DOWN' and li[i] < li[i + 1]:
      x = ""
      count += 1

print(count)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = 1
tmp = "0"
for i in range(n-1):
  if a[i] < a[i+1]:
    if tmp == "-":
      ans += 1
      tmp = "0"
    else:
      tmp ="+"
  elif a[i] > a[i+1]:
    if tmp == "+":
      ans += 1
      tmp = "0"
    else:
      tmp = "-"      

print(ans)

Solution (Python):
N = int (input())
A = [int(a) for a in input().split()]
d = 0
ans = 1
for i in range(1, N):
    if A[i-1] < A[i]:
        if d < 0:
            ans += 1
            d = 0
        elif d == 0:
            d = 1

    elif A[i-1] > A [i]:
        if d > 0:
            ans +=1
            d = 0
        elif d == 0:
            d = -1

print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
flag=0
count=1


for i in range(N-1):

    if flag==1 and A[i+1]<A[i] or flag==-1 and A[i+1]>A[i] or flag=="st":
        count+=1
        flag=0
    elif flag==0:
        if A[i+1]==A[i]:
            pass
        elif A[i+1]>A[i]:
            flag=1
        else:
            flag=-1


print(count)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

ans = 1
x = 0

for i in range(n-1):
    if a[i] < a[i+1]:
        if x == -1:
            ans += 1
            x = 0
        elif x == 0:
            x = 1
    elif a[i] > a[i+1]:
        if x == 1:
            ans += 1
            x = 0
        elif x == 0:
            x = -1
print(ans)

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
d = 0
ans = 1
for i in range(1, N):
    if A[i-1] < A[i]:
        if d < 0:
            ans += 1
            d = 0
        elif d == 0:
            d = 1
    elif A[i-1] > A[i]:
        if d > 0:
            ans += 1
            d = 0
        elif d == 0:
            d = -1
print(ans)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10


def solve():
    n = II()
    a = LI()
    ans = 1
    t = -1
    for i in range(n - 1):
        if a[i] < a[i + 1]:
            if t == 1:
                continue
            elif t == -1:
                t = 1
            else:
                t = -1
                ans += 1
        elif a[i + 1] < a[i]:
            if t == 0:
                continue
            elif t == -1:
                t = 0
            else:
                t = -1
                ans += 1
    print(ans)
    return



if __name__ == '__main__':
    solve()

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
flg = 0
cnt=0
for i in range(n-1):
    if l[i+1]==l[i]:
        continue
    elif l[i+1]>l[i]:
        if flg==-1:
            cnt+=1
            flg=0
        else:
            flg=1
    else:
        if l[i+1]<l[i]:
            if flg==1:
                cnt+=1
                flg=0
            else:
                flg=-1
print(cnt+1)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
flg = 0
ans = 0
for i in range(1,n):
  if a[i] == a[i-1]:
    continue
  elif a[i] > a[i-1]:
    if flg == -1:
      ans += 1
      flg = 0
    else:
      flg = 1
  elif a[i] < a[i-1]:
    if flg == 1:
      ans += 1
      flg = 0
    else:
      flg = -1
print(ans+1)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
flg = 0
ans = 0
for i in range(1,n):
  if a[i] == a[i-1]:
    continue
  elif a[i] > a[i-1]:
    if flg == -1:
      ans += 1
      flg = 0
    else:
      flg = 1
  elif a[i] < a[i-1]:
    if flg == 1:
      ans += 1
      flg = 0
    else:
      flg = -1
print(ans+1)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
cnt = 0
flag = 0
for i in range(N-1):
    if A[i]==A[i+1]:
        continue
    elif A[i]<A[i+1]:
        if flag==0 or flag==1:
            flag = 1
            continue
        else:
            cnt += 1
            flag = 0
    elif A[i]>A[i+1]:
        if flag==0 or flag==-1:
            flag = -1
            continue
        else:
            cnt += 1
            flag = 0
print(cnt+1)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
u = False
d = False
res = 1
i = 0
while i < n-1:
    if u and a[i+1] < a[i]:
        res += 1
        u = False
        i += 1
    elif d and a[i+1] > a[i]:
        res += 1
        d = False
        i += 1
    if i < n-1:
        if a[i+1] > a[i]:
            u = True
        elif a[i+1] < a[i]:
            d = True
    i += 1
print(res)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))

cnt=0
bflag=False
sflag=False

for i in range(1,N):
  if A[i]>A[i-1]:
    if sflag==True:
      cnt+=1
      bflag=False
      sflag=False
    else:
      bflag=True
      sflag=False
  elif A[i]<A[i-1]:
    if bflag==True:
      cnt+=1
      bflag=False
      sflag=False
    else:  
      bflag=False
      sflag=True    
  else:
    pass
    
print(cnt+1)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
S = 0
p = 1
for i in range(N-1):
  if S == 0 and A[i] < A[i+1]:
    S = 1
  elif S == 0 and A[i] > A[i+1]:
    S = -1
  elif S == 1 and A[i] > A[i+1]:
    S = 0
    p += 1
  elif S == -1 and A[i] < A[i+1]:
    S = 0
    p += 1
print(p)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ans=0
b=a[0]
up=True
down=True
for i in range(1,n):
    if b<a[i] and up:
        down=False
        b=a[i]
    elif b>a[i] and down:
        up=False
        b=a[i]
    elif b<a[i] and not(up):
        ans+=1
        up=True
        b=a[i]
    elif b>a[i] and not(down):
        ans+=1
        down=True
        b=a[i]
    else:
        continue
print(ans+1)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
cnt=0
cnt1=0
ans=1
aa=a[0]
for i in range(n):
    
    if aa>a[i] and cnt1==0:
        cnt=1
        aa=a[i]
    elif aa<a[i] and cnt==0:
        cnt1=1
        aa=a[i]
    elif aa>a[i] and cnt1!=0:
        aa=a[i]
        ans+=1
        cnt1=0
    elif aa<a[i] and cnt!=0:
        aa=a[i]
        ans+=1
        cnt=0
    else:
        continue
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N,*A = map(int,read().split())


B = [A[0]] + [y for x,y in zip(A,A[1:]) if x != y]

seq = []
answer = 0
for x in B:
    if not seq:
        answer += 1
        seq.append(x)
        continue
    if len(seq) == 1:
        seq.append(x)
        continue
    a,b = seq[-2:]
    if a < b < x or a > b > x:
        seq.append(x)
        continue
    
    answer += 1
    seq = [x]

print(answer)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
i=0
ans=0
if n<=2:
    print(1)
    exit()
while i<n-1:
    if a[i]<a[i+1]:
        i+=1
        while i<n-1:
            if a[i]<=a[i+1]:
                i+=1
            else:
                if i==n-2:
                    ans+=1
                break
        
        i+=1
        
        ans+=1
    elif a[i]>a[i+1]:
        i+=1
        while i<n-1:
            if a[i]>=a[i+1]:
                i+=1
            else:
                if i==n-2:
                    ans+=1
                break
        
        i+=1
        
        ans+=1
    else:
        i+=1
        
        if i==n-1:
            ans+=1
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

up = False
down = False
count = 1
for i in range(N-1):
    if A[i] < A[i+1]:
        up = True
    elif A[i] > A[i+1]:
        down = True
    if up ==True and down == True:
        count += 1
        up = False
        down = False
print(count)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))

plus = True
minus = True

ans = 0
for i in range(1, n):
    if plus and minus:
        if A[i] - A[i-1] > 0:
            minus = False
        elif A[i] - A[i-1] < 0:
            plus = False
    if plus and not minus and A[i] - A[i-1] < 0:
        ans += 1
        minus = True
    elif not plus and minus and A[i] - A[i-1] > 0:
        ans += 1
        plus = True

ans += 1

print(ans)

Solution (Python):

N = int(input())
a = list(map(int, input().split(' ')))

def dec(i):
    while i+1 < N and a[i] >= a[i+1]:
        i += 1
    return i

def inc(i):
    while i+1 < N and a[i] <= a[i+1]:
        i += 1
    return i


ans = 0
i = 0
while i < N:
    i = max(inc(i), dec(i)) + 1
    ans += 1

print(ans)

Solution (Python):





N = input()
a = [int(i) for i in input().split()] 
result = 0
first_flag = 1

for i in range(1,len(a)):
    if first_flag == 1:
        if a[i-1] > a[i]:
            first_flag = 2
        elif a[i-1] < a[i]:
            first_flag = 3
    elif first_flag == 2:
        if a[i-1] < a[i]:
            result += 1
            first_flag = 1
    elif first_flag == 3:
        if a[i-1] > a[i]:
            result += 1
            first_flag = 1    

print(result+1)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))

d=False
u=False

cnt=0

for i in range(0,n-1):
  l=a[i];r=a[i+1]
  if l<r:u=True
  elif l>r:d=True
    
  if u and d:
    cnt+=1
    u=False;d=False
print(cnt+1)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
ret = 1
up, down = False, False
for i in range(N - 1):
    if A[i] < A[i + 1]:
        up = True
    elif A[i] > A[i + 1]:
        down = True
    if up == down == True:
        ret += 1
        up, down = False, False
print(ret)

Solution (Python):
n = int(input())
line = list(map(int,input().split()))
count = 1
up = False
down =False
for i in range(n - 1):
  if line[i] < line[i + 1]:
    up = True
  elif line[i] > line[i + 1]:
    down = True
  if up == down == True:
    count += 1
    up =False
    down =False
print(count)

Solution (Python):

n = int(input())
a = list(map(int, input().split()))

di = ""
count = 0
i = 0
while i != n - 1:
    if a[i] == a[i+1] and di == "":
        di = ""
    elif a[i] == a[i+1]:
        di = di 

    if a[i] < a[i+1] and di == "":
        di = "増加"
    if a[i] > a[i+1] and di == "":
        di = "減少"

    if di == "増加" and a[i] > a[i+1]:
        count += 1
        di = ""
    if di == "減少" and a[i] < a[i+1]:
        count += 1
        di = ""

    i += 1
print(count + 1)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
flag=0
ans=1
for i in range(1,N):
  if flag== 0:
    if a[i]>a[i-1]:
      flag=1
      continue
    elif a[i]<a[i-1]:
      flag=-1
      continue
    else:
      continue
  elif flag>0:
    if a[i]<a[i-1]:
      ans+=1
      flag=0
  else:
    if a[i]>a[i-1]:
      ans+=1
      flag=0
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
flag = None
c = 1 
def check(i):
        for j in range(i, N - 1): 
                if A[j] - A[j + 1] < 0:
                        return 1 
                elif A[j] - A[j + 1] > 0:
                        return -1
flag = check(0)
for i in range(N - 1): 
        if flag == None:
                break
        elif flag == 1:
                if A[i] - A[i + 1] > 0:
                        c += 1
                        flag = check(i + 1)
        elif flag == -1: 
                if A[i] - A[i + 1] < 0:
                        c += 1
                        flag = check(i + 1)
print(c)

Solution (Python):
n=int(input())
s=list(map(int,input().split()))
cnt=1
u=False
d=False
for i in range(n-1):
    if s[i]<s[i+1]:
        u=True
    elif s[i]>s[i+1]:
        d=True
    if u and d:
        cnt+=1
        u=False
        d=False
print(cnt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = []

flag = 0  
ans = 1
cur = a[0]

for i in a:
    if (flag == 1 and i < cur) or (flag == 2 and i > cur):
        ans += 1
        flag = 0
    elif flag != 1 and cur < i:
        flag = 1
    elif flag != 2 and cur > i:
        flag = 2
    cur = i
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = 1
flag = None
for i in range(N-1):
  if flag is None:
    flag = 1 if A[i]<A[i+1] else -1 if A[i]>A[i+1] else 0
  else:
    if flag == 0:
      if A[i]<A[i+1]: flag = 1
      elif A[i]>A[i+1]: flag =- 1
    elif A[i]<A[i+1] and flag == -1:
      ans += 1
      flag = None
    elif A[i]>A[i+1] and flag == 1:
      ans += 1
      flag = None
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

ans = 1
up, dn = False, False
for i in range(N-1):
    if A[i] < A[i+1]:
        up = True
    elif A[i] > A[i+1]:
        dn = True
    if up and dn:
        ans += 1
        up, dn = False, False
print(ans)

Solution (Python):


def main()->None:
    
    N = int(input())
    A = list(map(int, input().split()))

    res = 0
    i = 0
    while i < N  :
        
        while (i+1 < N) and (A[i] == A[i+1]):
            i += 1 
        
        if (i+1 < N) and (A[i] < A[i+1]):
            while (i+1 < N) and (A[i] <= A[i+1]):
                i += 1
        
        elif (i+1 < N) and A[i] > A[i+1]:
            while (i+1 < N) and (A[i] >= A[i+1]):
                i += 1
        res += 1
        i += 1
    print(res)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
B = [A[0]]
for i in range(1, N) :
  if A[i] != B[-1] :
    B.append(A[i])

N = len(B)

ret = 1
i = 2
while i < N :
  if (B[i] - B[i-1]) * (B[i-1] - B[i-2]) < 0 :
    ret += 1
    i += 1
  i += 1

print(ret)

Solution (Python):
n = int(input())

l = list(map(int,input().split()))

flag = 0
ans = 0

for i in range(1,n):
	
	
	if flag == 0:
		if l[i] > l[i-1]:
			flag = 1
		elif l[i] < l[i-1]:
			flag = -1
		else:
			pass
						
	elif flag == 1:
		if l[i] > l[i-1]:
			pass
		elif l[i] == l[i-1]:
			pass
		elif l[i] < l[i-1]:
			ans = ans + 1
			flag = 0
			
	elif flag == -1:
		if l[i] < l[i-1]:
			pass
		elif l[i] == l[i-1]:
			pass
		elif l[i] > l[i-1]:
			ans = ans + 1
			flag = 0		

print(ans+1)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

if n == 1:
    print(1)
    exit()

mode = 0
ans = 1
for i in range(1, n):
    if mode == 0:
        if a[i] > a[i-1]:
            mode = 1
        elif a[i] < a[i-1]:
            mode = -1
    elif mode == 1:
        if a[i] < a[i-1]:
            ans += 1
            mode = 0
    else:
        if a[i] > a[i-1]:
            ans += 1
            mode = 0

print(ans)

Solution (Python):
n = int(input())
L = list(map(int,input().split()))

inc = 0
cnt = 0
for i in range(n-1):
    if inc == -1 and L[i] < L[i+1]:
        cnt += 1
        inc = 0
    elif inc == 1 and L[i] > L[i+1]:
        cnt += 1
        inc = 0
    elif inc == 0 and L[i] > L[i+1]:
        inc = -1
    elif inc == 0 and L[i] < L[i+1]:
        inc = 1
print(cnt+1)

Solution (Python):

N=int(1e5+10)
n=input()
a=map(int,raw_input().split())
ans=1;lst=a[0];up=-1;
for i in range(1,len(a)):
    if up==-1:
        if(lst>a[i]): up=0
        if(lst<a[i]): up=1
    else:
        if(up==0 and lst<a[i]): ans=ans+1;up=-1
        if(up==1 and lst>a[i]): ans=ans+1;up=-1
    lst=a[i]
print ans

Solution (Python):
n=int(input())
a=list(map(int,input().split()))

cnt=1
s=0
for i in range(1,len(a)):
 if a[i-1]!=a[i]:
  if s==0 and a[i-1]<a[i]:
   s=1
  elif s==0 and a[i-1]>a[i]:
   s=-1
  elif s==-1 and a[i-1]<a[i]:
   s=0
   cnt+=1
  elif s==1 and a[i-1]>a[i]:
   s=0
   cnt+=1
print(cnt)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
import numpy as np
count=0
i=0
while i < n:
  while i<n-1:
    if a[i+1]==a[i]:
      i+=1
    else:
      break
  
  if i<n-1 and a[i+1]>a[i]:
    while i<n-1 and a[i+1]>=a[i]:
      i+=1
  elif i<n-1 and a[i+1]<a[i]:
    while i<n-1 and a[i+1]<=a[i]:
      i+=1
  count+=1
  i+=1
  

print(count)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = 0
if n <= 2:
    print(1)
    exit()
pre = a[1] - a[0]
i = 2
while i < n:
    if pre * (a[i] - a[i-1]) < 0:
        ans += 1
        pre = 0
        i += 1
        if i == n:
            break
    if a[i] != a[i-1]:
        pre = a[i] - a[i-1]
    i += 1
print(ans + 1)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

ans = 0
i = 0

while i < n:
    if i == n - 1:
        ans += 1
        break

    
    plus = 0
    while i + plus + 1 < n:
        if a[i + plus] <= a[i + plus + 1]:
            plus += 1
        else:
            break

    
    minus = 0
    while i + minus+1 < n:
        if a[i + minus] >= a[i + minus+1]:
            minus += 1
        else:
            break

    ans += 1
    if plus >= minus:
        i += plus + 1
    else:
        i += minus + 1


print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

mode = 0
ans = 0
for i in range(1, n):
    if a[i]-a[i-1] == 0:
        continue
    if a[i]-a[i-1] > 0:
        if mode == 0:
            mode = 1
        if mode == -1:
            ans += 1
            mode = 0
    if a[i]-a[i-1] < 0:
        if mode == 0:
            mode = -1
        if mode == 1:
            ans += 1
            mode = 0
print(ans+1)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
ans = 0
f = -1
for i in range(n):
  if i == n-1:
    ans += 1
  else:
    if A[i] != A[i+1]:
      if f == -1:
        f = A[i] < A[i+1]
      else:
        if f ^ (A[i] < A[i+1]):
          ans += 1
          f = -1
print(ans)

Solution (Python):
N, *A = map(int, open(0).read().split())
d = 0
ans = 1
for i in range(N-1):
  if d == 0 and A[i] != A[i+1]:
    d = - 1 + 2 * (A[i] < A[i+1])
  elif d == 1 and A[i] > A[i+1]:
    d = 0
    ans += 1
  elif d == -1 and A[i] < A[i+1]:
    d = 0
    ans += 1
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

before = A[0]
before_ = "="
count = 1
for idx, a in enumerate(A[1:]):
    if before == a:
        now = before_
    if before < a:
        now = "+"
    if before > a:
        now = "-"

    if (before_ == "+" and now == "-") or (before_ == "-" and now == "+"):
        count += 1
        before_ = "="
    else:
        before_ = now
    before = a
print(count)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
cnt=0
cnt1=0
ans=1
aa=a[0]
for i in range(n):
    
    if aa>a[i] and cnt1==0:
        cnt=1
        aa=a[i]
    elif aa<a[i] and cnt==0:
        cnt1=1
        aa=a[i]
    elif aa>a[i] and cnt1!=0:
        aa=a[i]
        ans+=1
        cnt1=0
    elif aa<a[i] and cnt!=0:
        aa=a[i]
        ans+=1
        cnt=0
    else:
        continue
print(ans)

Solution (Python):


import numpy as np
import scipy

N = int(input())




A = list(map(int, input().split()))

split = 0
order = ""
for i in range(len(A)-1):
    if order == "up" and A[i] > A[i+1]:
        split += 1
        order = ""
    elif order == "down" and A[i] < A[i+1]:
        split += 1
        order = ""
    elif order == "":
        if A[i] < A[i+1]:
            order = "up"
        elif A[i] > A[i+1]:
            order = "down"

print(split+1)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
fp=A[0];ans=1;fp=fn=False
for i in range(1,N):
    if A[i-1]<A[i]:
        if not fp and not fn:
            fp=True
        elif fn:
            ans+=1
            fn=False
    if A[i-1]>A[i]:
        if not fp and not fn:
            fn=True
        elif fp:
            ans+=1
            fp=False

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

last = a[0]
direction = 0

ans = 1

for x in a[1:]:
    if direction == 0:
        if last < x:
            direction = 1
        elif last > x:
            direction = -1
        last = x
    elif direction == 1:
        if last > x:
            direction = 0
            ans += 1
        last = x
    elif direction == -1:
        if last < x:
            direction = 0
            ans += 1
        last = x

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

ans = 1
up = None

for x, y in zip(a, a[1:]):
    if x < y:
        if up is None:
            up = True
        elif not up:
            ans += 1
            up = None
    elif x > y:
        if up is None:
            up = False
        elif up:
            ans += 1
            up = None
print(ans)