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):
N=int(input())
A=list(map(int, input().split()))

flag=0 
cnt=0

for i in range(N-1):
    if A[i+1]-A[i] >0:
        if flag ==1:
            continue
        elif flag ==-1:
            flag =0
            cnt+=1
        elif flag ==0:
            flag =1
    elif A[i+1]-A[i] <0:
        if flag ==-1:
            continue
        elif flag ==1:
            flag =0
            cnt+=1
        elif flag ==0:
            flag =-1
    elif A[i+1]-A[i] ==0:
        if flag == 0:
            flag = 0
        elif flag ==1:
            flag =1
        elif flag ==-1:
            flag =-1
print(cnt+1)

Solution (Python):
n = int(input())
a_l = list(map(int, input().split()))
ans = 1
is_plus = None
sub = []
for i in a_l:
    if len(sub) == 0:
        sub.append(i)
        continue
    if i == sub[-1]:
        sub.append(i)
        continue

    if is_plus is None:
        if sub[-1] > i:
            is_plus = False
        else:
            is_plus = True
        sub.append(i)

    elif is_plus is True:
        if sub[-1] > i:
            sub = [i]
            ans += 1
            is_plus = None
        else:
            sub.append(i)
    else:
        if sub[-1] < i:
            sub = [i]
            ans += 1
            is_plus = None
        else:
            sub.append(i)
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
aset=set(a)
ans=1
if len(aset)>1:
    num1=a[0]
    num2=a[1]
    num3=2 
    if num1<num2:
        num3=1
    if num1>num2:
        num3=0
    for i in range(len(a)-2):
        num=a[2+i]
        if num3==2 and num2>num:
            num3=0
        if num3==2 and num2<num:
            num3=1
        if num3==0 and num2<num:
            ans+=1
            num3=2
        if num3==1 and num2>num:
            ans+=1
            num3=2
        num2=num
print(ans)

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


p = 0
res = 0

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

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = list(map(int, input().split()))
l = []
prev = None
for i in range(n):
    if prev!=a[i]:
        l.append(a[i])
    prev = a[i]
        
if len(l)==1:
    ans = 1
else:
    m = len(l)
    prev = None
    ans = 0
    for i in range(m-1):
        s = l[i]<l[i+1]
        if prev is None:
            prev = s
        elif prev!=s:
            ans += 1
            prev = None
    ans += 1
print(ans)

Solution (Python):
n,*a=map(int,open(0).read().split())
ans=1
vec=0
for i in range(n-1):
    now=a[i+1]-a[i]
    if vec*now<0:
        ans+=1
        vec=0
    elif vec==0:
        vec=now
print(ans)

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

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

def f():
    for i in range(1, N):
        if A[i-1] < A[i]:
            state = 0
            break
        if A[i-1] > A[i]:
            state = 1
            break
    else:
        print(1)
        return

    cnt = 1

    for i in range(1, N):
        if A[i-1] < A[i]:
            if state == 1:
                state = 2
                cnt += 1
            else:
                state = 0

        if A[i-1] > A[i]:
            if state == 0:
                state = 2
                cnt += 1
            else:
                state = 1
    
    print(cnt)

f()

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
ans = 1

check = 0
before = a[0]
for i in a[1:]:
    if i == before:
        continue
    if before > i:
        if check == 0:
            check = 1
        elif check == -1:
            ans += 1
            check = 0
    else:
        if check == 0:
            check = -1
        elif check == 1:
            ans += 1
            check = 0
    before = i

print(ans)

Solution (Python):
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()

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

if inv == 1:
    ans += 1
    
print(ans)

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

if N == 1:
  print(1)
else:
  ans = 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
    
    ans += 1
    i += 1
      
  print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
c=1
g=0
for i in range(n-1):
  if (g==1 and a[i+1]<a[i]) or (g==-1 and a[i+1]>a[i]) :
    c+=1
    g=0
  elif g==0:
    if a[i+1]>a[i]:
      g=1
    elif a[i+1]<a[i]:
      g=-1
print(c)

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007



def main():
	n = int(input())
	
	
	al = lin()
	flg=None
	dd = 1
	pv=al[0]
	for v in al[1:]:
		if flg is None:
			if pv == v:
				pass
			elif pv < v:
				flg = 'up'
			else:
				flg = 'down'
		elif flg == 'up':
			if v < pv:
				dd += 1
				flg = None
		else:
			if v > pv:
				dd += 1
				flg = None
		pv = v
	print(dd)
				
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

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

li = []

for i in range(N-1):
    if A[i+1] - A[i] > 0:
       li.append('+')
    elif A[i+1] - A[i] < 0:
        li.append('-')
    else:
        pass

ans = 1
p = 0
m = 0
for i in li:
    if i == '+':
        p = 1
    elif i == '-':
        m = 1
    if p == 1 & m == 1:
        ans += 1
        p = 0
        m = 0
print(ans)

Solution (Python):






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





ans = 1
p = 0
m = 0
pre = A[0]
for a in A[1:]:
    if a > pre:
        p = 1
    elif a < pre:
        m = 1
    if p == 1 and m == 1:
        ans += 1
        p = 0
        m = 0
    pre = a
print(ans)

Solution (Python):
p = -1
xp = -1
ans = 1
input()
for i in input().split(" "):
  i = int(i)
  if i == p: continue
  if p != -1:
    x = p < i
    if xp != -1:
      ans += x != xp
    if xp != -1 and x != xp:
      xp = -1
    else:
      xp = x
  p = i
print(ans)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
ans = 1
p = m = 0
c = A[0]
for i in A[1:]:
    if i > c:
        p = 1
    elif i < c:
        m = 1
    if p == 1 and m == 1:
        p = m = 0
        ans += 1
    c = i
print(ans)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
if N<=2:
    print(1)
    exit()

now=0
ans=1
for i in range(1,N):
    if i==1:
        if A[i]-A[i-1]==0:
            now=0
        else:
            now=(A[i]-A[i-1])//(abs(A[i]-A[i-1]))
        if now>=1:
            plus=True
        elif now==0:
            plus=-1
        else:
            plus=False
            
    else:
        if A[i]-A[i-1]==0:
            nex=0
        else:
            nex=(A[i]-A[i-1])//(abs(A[i]-A[i-1]))
        if plus==-1:
            if nex>=1:
                plus=True
            elif nex<0:
                plus=False
        elif plus:
            if nex<0:
                plus=-1
                ans+=1
        else:
            if nex>0:
                plus=-1
                ans+=1

print(ans)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
ans = 1
p = m = 0
c = A[0]
for i in A[1:]:
    if i > c:
        p = 1
    elif i < c:
        m = 1
    if p == 1 and m == 1:
        p = m = 0
        ans += 1
    c = i
print(ans)

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
cnt,i = 0,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
    cnt+=1
    i+=1
print(cnt)

Solution (Python):
num = int(input())
l = list(map(int, input().split()))
count = 1
var = var2 = 0
c = l[0]
for i in l[1:]:
    if c < i:
        var = 1
    elif c > i:
        var2 = 1
    if var == 1 and var2 == 1:
        count += 1
        var = var2 = 0
    c = i
print(count)

Solution (Python):
num = int(input())
a = list(map(int, input().split()))
k = 1
x = y = 0
c = a[0]
for i in a[1:]:
    if c < i:
        x = 1
    elif c > i:
        y = 1
    if x == 1 and y == 1:
        k += 1
        x = y = 0
    c = i
print(k)

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

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

i = 0
res = 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)

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

ans = 1
status = 0
prev = a[0]

for i in a:
    diff = i - prev

    if diff == 0:
        pass
    elif diff > 0:
        if status >= 0:
            status = diff
        else:
            ans += 1
            status = 0
    else:
        if status <= 0:
            status = diff
        else:
            ans += 1
            status = 0

    prev = i

print(ans)

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

ans = 1
mode = 0
prev = A[0]

for i in A:
    check = i - prev

    if check == 0:
        pass
    elif check > 0:
        if mode >= 0:
            mode = check
        else:
            ans += 1
            mode = 0
    else:
        
        if mode <= 0:
            mode = check
        else:
            ans += 1
            mode = 0

    prev = i

print(ans)

Solution (Python):
import sys
N = int(input())
A = [int(x) for x in input().split()]
if N == 1:
    print(1)
    sys.exit()

inc = 0  
ans = 1
for i in range(N - 1):
    if A[i] < A[i + 1]:
        if inc == -1:
            ans += 1
            inc = 0
        elif inc == 0:
            inc = 1
    elif A[i] > A[i + 1]:
        if inc == 1:
            ans += 1
            inc = 0
        elif inc == 0:
            inc = -1

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
k = 1
x = y = 0
c = A[0]
for i in A[1:]:
    if c < i:
        x = 1
    elif c > i:
        y = 1
    if x == 1 and y == 1:
        k += 1
        x = y = 0
    c = i
print(k)

Solution (Python):
import sys
N = int(input())
A = [int(x) for x in input().split()]
if N == 1:
    print(1)
    sys.exit()

inc = 0  
ans = 1
for i in range(N - 1):
    if A[i] < A[i + 1]:
        if inc == -1:
            ans += 1
            inc = 0
        elif inc == 0:
            inc = 1
    elif A[i] > A[i + 1]:
        if inc == 1:
            ans += 1
            inc = 0
        elif inc == 0:
            inc = -1

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
k = 1
x = y = 0
c = A[0]
for i in A[1:]:
    if c < i:
        x = 1
    elif c > i:
        y = 1
    if x == 1 and y == 1:
        k += 1
        x = y = 0
    c = i
print(k)

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

ans = 1
up = dn = False
pre = A[0]
for a in A[1:]:
    if pre < a:
        up = True
    elif pre > a:
        dn = True
    if up and dn:
        ans += 1
        up = dn = False
    pre = a

print(ans)

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

ans = 1
up = dn = False
pre = A[0]
for a in A[1:]:
    if pre < a:
        up = True
    elif pre > a:
        dn = True
    if up and dn:
        ans += 1
        up = dn = False
    pre = a

print(ans)

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

i = 0
ans = 0

while i < n:
    i1 = i
    last = a[i]
    while i1 < n:
        if last <= a[i1]:
            last = a[i1]
            i1 += 1
        else: break
    
    i2 = i
    while i2 < n:
        if last >= a[i2]:
            last = a[i2]
            i2 += 1
        else: break
    
    i = max(i1, i2)
    ans += 1

print(ans)

Solution (Python):
import sys
N = int(input())
a = list(map(int, input().split()))
num, cut = 0, 0

if N == 1:
    print(1)
    sys.exit()

for i in range(N-1):
    if a[i] == a[i+1]:
        continue
    if a[i] < a[i+1] and num == 0:
        num  = 1
        cut += 1
    elif a[i] > a[i+1] and num == 0:
        num = -1
        cut += 1
    elif num == 1 and a[i] > a[i+1]:
        if i == N-2:
            cut += 1
        num = 0
    elif num == -1 and a[i] < a[i+1]:
        if i == N-2:
            cut += 1
        num = 0
print(cut)

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())


dp = 1, 1
for i in range(1, N):
    if A[i] > A[i-1]:
        dp = min(dp)+1, min(dp[0]+1, dp[1])
    elif A[i] == A[i-1]:
        pass
    else:
        dp = min(dp[0], dp[1]+1), min(dp)+1

print(min(dp))

Solution (Python):
def main():
    N = int(input())
    A = [int(a) for a in input().split()]

    idx = 0
    ans = 0
    for i in range(len(A)):
        if idx + 1 > len(A) - 1 :
            break

        m = ""
        if A[idx] < A[idx + 1]:
            m = "+"
        elif A[idx] > A[idx + 1]:
            m = "-"
        else:
            find = False
            for j in range(idx, len(A)-1):
                if A[j] < A[j + 1]:
                    m = "+"
                    find = True
                    idx = j
                    break
                elif A[j] > A[j + 1]:
                    m = "-"
                    find = True
                    idx = j
                    break
            if find == False:
                print(ans + 1)
                exit()

        for j in range(idx, len(A) - 1):
            if m == "+":
                if A[j] <= A[j + 1]:
                    idx = j + 1
                else:
                    idx = j + 1
                    ans += 1
                    break
            elif m == "-":
                if A[j] >= A[j + 1]:
                    idx = j + 1
                else:
                    idx = j + 1
                    ans += 1
                    break

    print(ans + 1)

if __name__ == "__main__":
    main()

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

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


flg=0
cnt=0

tmp=[]
for i in range(1,n):
    if a[i]>a[i-1]:
        tmp.append(1)
    elif a[i]==a[i-1]:
        tmp.append(0)
    else:
        tmp.append(-1)
flg=0
swi=0
for j in range(len(tmp)):
    if swi==1:
        flg=tmp[j]
        swi=0
        continue
    
    if flg==0:
        flg=tmp[j]
    elif flg==1:
        if tmp[j]==-1:
            cnt+=1
            flg=-1
            swi=1
    elif flg==-1:
        if tmp[j]==1:
            cnt+=1
            flg=1
            swi=1

print(cnt+1)

Solution (Python):
from operator import lt, gt, le, ge
def get_monotonic_section(A, start=0, operator=lt):
    a0 = A[start]
    for i in range(start+1, len(A)):
        a = A[i]
        if not operator(a0, a):
            return i
        a0 = a
    return len(A)

n, *A = map(int, open(0).read().split())
c = t = 0
while t < n:
    t1 = get_monotonic_section(A, start=t, operator=le)
    t2 = get_monotonic_section(A, start=t, operator=ge)
    t = max(t1, t2)
    c += 1
print(c)

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

up = False
down = False
ans = 1

for i in range(1, n):
    if up:
        if a[i] < a[i-1]:
            ans += 1
            up = False
    elif down:
        if a[i] > a[i-1]:
            ans += 1
            down = False
    else:
        if a[i] > a[i-1]:
            up = True
        elif a[i] < a[i-1]:
            down = True

print(ans)

Solution (Python):

import collections
















import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
S = readInts()
i = 0
cnt = 0
while i < n:
    while i+1 < n:
        if S[i] == S[i+1]:
            i += 1
        else:
            break
    if i + 1 < n:
        if S[i] < S[i+1]:
            while i + 1 < n:
                if S[i] <= S[i+1]:
                    i += 1
                else:
                    break
        else:
            while i + 1 < n:
                if S[i] >= S[i+1]:
                    i += 1
                else:
                    break
    cnt += 1
    i += 1
print(cnt)

Solution (Python):
_, *A = map(int, open(0).read().split())
ans = 1
is_add = False
is_minus = False
tmp = A[0]
for a in A[1:]:
  if tmp > a:
    if is_add:
      ans += 1
      is_add = False
    else:
      is_minus = True
  elif tmp < a:
    if is_minus:
      ans += 1
      is_minus = False
    else:
      is_add = True
  tmp = a
print(ans)

Solution (Python):

n = int(input())
a = list(map(int,input().split()))
cnt = 0
first = True
up = False
down = False
for i in range(n):
    
    if first:
        cnt += 1
        now = a[i]
        if i < n-1:
            
            if a[i+1] > now:
                
                up = True
                down = False
                first = False
            elif a[i+1] < now:
                
                down = True
                up = False
                first = False
            else:
                
                up = False
                down = False
                first = True
                cnt -= 1

            
    else:
        now = a[i]
        if i < n-1:
            if up:
                if now > a[i+1]:
                    first = True

            elif down:
                if now < a[i+1]:
                    first = True

print(cnt)

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
x = 0 
i = 0 
ans = 0

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

while True:
    if x == 0:
        if li[i] < li[i+1]:
            x = 1
        elif li[i] > li[i+1]:
            x = -1
        else:
            x = 0
    if x == 1:
        if li[i] > li[i+1]:
            ans += 1
            x = 0
    if x == -1:
        if li[i] < li[i+1]:
            ans += 1
            x = 0
    i += 1
    if i == n-1:
        break
print(ans+1)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]

if N == 1:
    print(1)
else:
    pre = A[0]
    flg = 0
    ans = 1
    for i in range(1, N):
        now = A[i]
        diff = now - pre
        if flg == 1:
            if diff >= 0:
                pass
            else:
                ans += 1
                flg = 0
        elif flg == -1:
            if diff <= 0:
                pass
            else:
                ans += 1
                flg = 0
        else:
            if diff > 0:
                flg = 1
            elif diff < 0:
                flg = -1
            else:
                pass
        pre = now
        
    print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

N = INT()
A = LIST()

ans = 1
tmp = 0
for i in range(1, N):
	if A[i] == A[i-1]:
		continue
	elif tmp == 0:
		tmp = (A[i]-A[i-1])//abs(A[i]-A[i-1])
	elif tmp != (A[i]-A[i-1])//abs(A[i]-A[i-1]):
		ans += 1
		tmp = 0

print(ans)

Solution (Python):



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

    ret = 1
    state = 0
    for i in range(1,N):

        if A[i]>A[i-1]:
            if state == -1:
                ret += 1
                state = 0
            else:
                state = 1
        elif A[i]<A[i-1]:
            if state == 1:
                ret += 1
                state = 0
            else:
                state = -1

    print(ret)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
now = A[0]
kind = 0
ans = 0
for i in range(1,N):
    if kind == 0:
        if now < A[i]:
            kind = 1
        elif now > A[i]:
            kind = 2
    elif kind == 1: 
        if now > A[i]:
            ans += 1
            kind = 0
    else: 
        if now < A[i]:
            ans += 1
            kind = 0
    now = A[i]
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+1]-A[i] > 0:
    if flag == 1:
      cnt +=0
    elif flag == -1:
      cnt +=1
      flag = 0
    elif flag ==0:
      flag = 1
  elif A[i+1]-A[i] < 0:
    if flag == 1:
      cnt +=1
      flag =0
    elif flag ==-1:
      cnt +=0
    elif flag ==0:
      flag = -1
  elif A[i+1]-A[i] == 0:
    flag += 0
      
    
print(cnt+1)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')
AtoZ = "abcdefghijklmnopqrstuvwxyz"






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count




N = I()
A = IL()

if N < 3:
  print(1)
else:
  status = "Pending"
  prev = A[0]
  count = 1
  for a in A:
    if status == "Pending":
      if a > prev:
        status = "UP"
      if a < prev:
        status = "DOWN"
    elif status == "UP":
      if a < prev:
        status = "Pending"
        count += 1
    else:
      if a > prev:
        status = "Pending"
        count += 1
    prev = a
  print(count)

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

i = 0
ans = 0
for a in A:
  right, right2 = i, i
  while right+1 < N and A[right] <= A[right+1]: right += 1
  while right2+1 < N and A[right2] >= A[right2+1]: right2 += 1
  i = max(right, right2) + 1
  ans += 1
  if i >= N: break
print(ans)

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

Solution (Python):


n = int(input())
a = list(map(int, input().split()))
up, down = 0, 0
ans = 0

for i in range(1, n):
    if a[i] > a[i-1]:
        up += 1
    if a[i-1] > a[i]:
        down += 1
    if up * down > 0:
        ans += 1
        up = 0
        down = 0
print(ans + 1)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cnt = 0
f = 0
for i in range(1, n):
    if a[i-1] < a[i]:
        ff = 1
    elif a[i-1] > a[i]:
        ff = -1
    else:
        ff = 0
    if f and ff and f != ff:
        cnt += 1
        f = 0
    elif not f:
        f = ff

print(cnt + 1)

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    end = len(A)
    judge = 0
    ans = 0
    i = 0

    if end == 1:
        return 1

    while True:
        if A[i] < A[i+1]:
            ans += 1
            while True:
                i += 1
                if i == end-1:
                    return ans
                elif A[i] <= A[i+1]:
                    continue
                else:
                    i += 1
                    if i == end-1:
                        ans += 1
                        return ans
                    else:
                        break

        elif A[i] > A[i+1]:
            ans += 1
            while True:
                i = i+1
                if i == end-1:
                    return ans
                elif A[i] >= A[i+1]:
                    continue
                else:
                    i += 1
                    if i == end-1:
                        ans += 1
                        return ans
                    else:
                        break

        elif A[i] == A[i+1]:
            i += 1
            if i == end-1:
                ans += 1
                return ans
            else:
                continue

print(main())

Solution (Python):
from collections import  deque
n=int(input())
a=list(map(int,input().split()))
cnt=0
judge=[]
for i in range(n-1):
    if a[i]<a[i+1]:
        flag=0
    elif a[i]>a[i+1]:
        flag=1
    elif a[i]==a[i+1]:
        continue
    if judge==[]:
        judge.append(flag)
    else:
        ff=judge.pop()
        if flag==ff:
            judge.append(flag)
        else:
            cnt+=1

print(cnt+1)

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